在我的应用程序中有一个imageViewA(框架{0,0,320,200}),我想用imageViewA的中心剪切一个圆,半径= 50,并绘制到另一个imageViewB(框架{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有空白区域.
如何正确剪辑此图片?
谢谢!
imageViewB.layer.cornerRadius = 50.0; imageViewB.layer.masksToBounds = YES;