gpt4 book ai didi

objective-c - 创建 UIImage 抠图

转载 作者:行者123 更新时间:2023-11-28 22:57:28 30 4
gpt4 key购买 nike

我有一个包含 2 个 UIImageViewsUIView - 一个框架和框架后面的图片。框架不是矩形 - 它是不规则形状。用户可以操纵框架后面的图片(缩放、旋转和平移),完成后,我想在框架内捕获图片的剪切 - 而不是图片和框架在一起。我有办法做到这一点吗?

我已经设法将图片和框架拼合成一个图像,如下所示,但我只想要图片,如果成功提取,它将有一个框架形状的边框。

- (IBAction)renderPhoto:(id)sender {
//Combine the layers into a single image
UIView *canvas = [[[sender superview] subviews] objectAtIndex:0];
UIGraphicsBeginImageContext(canvas.bounds.size);
[canvas.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

最佳答案

使用我的这个方法..

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{

UIGraphicsBeginImageContext(photo.frame.size);/// use your screen or photo frame
[photo.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();



CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];

/* no origin .....
UIGraphicsBeginImageContext(tempview.frame.size);
[[self.photo layer] renderInContext:UIGraphicsGetCurrentContext()];
cropped= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
*/
return cropped;
}

希望,这对你有帮助......:)

关于objective-c - 创建 UIImage 抠图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10517589/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com