3

アプリに imageViewA (frame{0,0,320,200}) があり、imageViewA の中心、radius=50 で円をクリップし、別の imageViewB (frame{0,0,100,100}) に描画します。このような元の画像効果:

ここに画像の説明を入力

以下のコードを使用してクリップします。

UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat height = self.bounds.size.height;
CGContextTranslateCTM(ctx, 0.0, height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, 50, 0, 2*M_PI, 0);
CGContextClosePath(ctx);
CGContextSaveGState(ctx);
CGContextClip(ctx);
CGContextDrawImage(ctx, CGRectMake(0,0,self.frame.size.width, self.frame.size.height), image.CGImage);
CGContextRestoreGState(ctx);
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
NSString *headerPath = [NSString stringWithFormat:@"%@/header.png",HomeDirectory];
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
if([imageData writeToFile:headerPath atomically:YES]){
    imageViewB.image = [UIImage imageWithContentsOfFile:headerPath];
}

次のような画像効果をクリップします。

ここに画像の説明を入力

サークル ビューのみが必要ですが、imageViewB に空白があるクリップ エフェクトが表示されます。この画像を正しくクリップするにはどうすればよいですか? ありがとう!

4

4 に答える 4

6
imageViewB.layer.cornerRadius = 50.0;
imageViewB.layer.masksToBounds = YES;
于 2014-01-07T23:41:59.867 に答える
2

JPEG は透明度をサポートしていません。UIImagePNGRepresentationの代わりに使用するとUIImageJPEGRepresentation、必要な結果が得られます。

NSData *imageData = UIImagePNGRepresentation(newImage);
if([imageData writeToFile:headerPath atomically:YES]){
    imageViewB.image = [UIImage imageWithContentsOfFile:headerPath];
}
于 2013-11-07T12:39:16.200 に答える
0

画像を描画する前に:

CGContextSetBlendMode(ctx, kCGBlendModeClear);
CGContextFillRect(ctx, self.bounds);
CGContextSetBlendMode(ctx, kCGBlendModeNormal);
于 2014-05-19T13:54:58.007 に答える