私は、その場でサークルを作成する方法を教えてくれるインターネット上の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;
いずれの方向にも感謝します。
'firstPieceView'はその内容を使って手動で別のビューを描画する場合は、ビュー階層に追加する – bshirley
'UIGraphicsGetCurrentContext()'を呼び出すために有効なコード内の唯一の場所は、 'UIView'の' drawInRect: 'のサブクラスの内側です。方法は、あなたがいる場所ですか? ----これは私が上記のコメントで仮定したものですが、私はあなたが円でクリップと画像をしようとしていると思われます** **画面上ではありません – bshirley
私はそれをクリップし、画面上の円にイメージします。私はカメラで写真を撮り、基本的にこの画像を円で切り取り、スクリーンに描きます。 – ilteris