2011-07-20 22 views
0

私は、その場でサークルを作成する方法を教えてくれるインターネット上の1つの例を見つけられず、このサークルを使用してUIImageをクリップします。CGContextAddEllipseInRect to CGImageRef to CGImageMaskCreate to CGContextClipToMask

ここに私のコードがありますが、残念ながら希望の結果が得られません。

//create a graphics context 
UIGraphicsBeginImageContext(CGSizeMake(243, 243)); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//create my object in this context 
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 243, 243)); 
CGContextSetFillColor(context, CGColorGetComponents([[UIColor whiteColor] CGColor])); 
CGContextFillPath(context); 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
//create an uiimage from the ellipse 


//Get the drawing image 
CGImageRef maskImage = [image CGImage]; 

// Get the mask from the image 
CGImageRef maskRef = CGImageMaskCreate(CGImageGetWidth(maskImage) 
            , CGImageGetHeight(maskImage) 
            , CGImageGetBitsPerComponent(maskImage) 
            , CGImageGetBitsPerPixel(maskImage) 
            , CGImageGetBytesPerRow(maskImage) 
            , CGImageGetDataProvider(maskImage) 
            , NULL 
            , false); 


//finally clip the context to the mask. 

CGContextClipToMask(context , CGRectMake(0, 0, 243, 243) , maskRef); 


//draw the image 
[firstPieceView.image drawInRect:CGRectMake(0, 0, 320, 480)]; 
// [firstPieceView drawRect:CGRectMake(0, 0, 320, 480)]; 

//extract a new image 
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 

NSLog(@"self.firstPieceView is %@", NSStringFromCGRect(self.firstPieceView.frame)); 
UIGraphicsEndImageContext(); 


self.firstPieceView.image = outputImage; 

いずれの方向にも感謝します。

+0

'firstPieceView'はその内容を使って手動で別のビューを描画する場合は、ビュー階層に追加する – bshirley

+0

'UIGraphicsGetCurrentContext()'を呼び出すために有効なコード内の唯一の場所は、 'UIView'の' drawInRect: 'のサブクラスの内側です。方法は、あなたがいる場所ですか? ----これは私が上記のコメントで仮定したものですが、私はあなたが円でクリップと画像をしようとしていると思われます** **画面上ではありません – bshirley

+0

私はそれをクリップし、画面上の円にイメージします。私はカメラで写真を撮り、基本的にこの画像を円で切り取り、スクリーンに描きます。 – ilteris

答えて

4

あなたの質問をよりよく言い換える必要があると思われます。 あなたが何をしようとしているのかにかかわらず、たくさんのサンプルコードがあります。ここで

あなたが楕円に午前の画像をクリップするカスタムUIViewサブクラスを実装できる方法は次のとおりです。

- (void)drawInRect:(CGRect)rect { 
    UIImage image;// set/get from somewhere 
    CGImageRef imageRef = [image CGImageRef]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextAddEllipseInRect(context, self.bounds); 
    CGContextClip(context); 
    CGContextDrawImage(context, self.bounds, imageRef); 
} 

買主の危険負担


編集(日後、自由時間が生成します):

- (void)drawRect:(CGRect)rect { 
    // we're ignoring rect and drawing the whole view 
    CGImageRef imageRef = [_image CGImage]; // ivar: UIImage *_image; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // set the background to black 
    [[UIColor blackColor] setFill]; 
    CGContextFillRect(context, self.bounds); 


    // modify the context coordinates, 
    // UIKit and CoreGraphics are oriented differently 
    CGContextSaveGState(context); 
    CGContextTranslateCTM(context, 0, CGRectGetHeight(rect)); 
    CGContextScaleCTM(context, 1, -1); 

    // add clipping path to the context, then execute the clip 
    // this is in effect for all drawing until GState restored 
    CGContextAddEllipseInRect(context, self.bounds); 
    CGContextClip(context); 

    // stretch the image to be the size of the view 
    CGContextDrawImage(context, self.bounds, imageRef); 
    CGContextRestoreGState(context); 
} 
+0

umm、上記のコードには問題があります。 UIImage *イメージ... CGImageRef imageRef = [イメージCGImage]; – ilteris

+0

上記のコードで画像を丸くすることはできません。なぜclipToMask? – ilteris

+0

私はそれをテストする時間がなかった、私の頭の上にあった_caveat emptor_の部分だった – bshirley