在我的应用中有一个 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有空白的剪辑效果显示。如何正确剪辑此图像?谢谢!