如何为 iOS 开发保存合成或修改后的图像?

发布于 2024-11-17 12:42:48 字数 929 浏览 1 评论 0原文

我向图像添加了叠加层(从相册中选择),但无法保存合成图像,以下代码仅保存原始图像。有人建议修改图像对象以添加图像吗?

    #pragma mark -
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{   
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
    overlay.alpha = 0.6;  // Customize the opacity of the top image.
    [imageView addSubview:overlay];
    CGContextRef context = UIGraphicsGetCurrentContext();
    //UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
        // Save image
    UIImageWriteToSavedPhotosAlbum(imageView.image, self,                                    @selector(image:didFinishSavingWithError:contextInfo:), context);
    [picker dismissModalViewControllerAnimated:YES];
}

I added an overlay to my image (selected from photo album) and I can't save the composite, the following code only saves the original image. Any one out have a suggestion to modify the image object to have the added image?

    #pragma mark -
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{   
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
    overlay.alpha = 0.6;  // Customize the opacity of the top image.
    [imageView addSubview:overlay];
    CGContextRef context = UIGraphicsGetCurrentContext();
    //UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
        // Save image
    UIImageWriteToSavedPhotosAlbum(imageView.image, self,                                    @selector(image:didFinishSavingWithError:contextInfo:), context);
    [picker dismissModalViewControllerAnimated:YES];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-11-24 12:42:48

UIImageWriteToSavedPhotosAlbum(imageView.image.layer, self)

UIImageWriteToSavedPhotosAlbum(imageView.image.layer, self)

梦魇绽荼蘼 2024-11-24 12:42:48

谢谢@拉胡尔维亚斯。我没有尝试你的建议,但我仍然感谢你的意见。最终在 Apple Dev 论坛的帮助下解决了这个问题。

这个问题的答案是,因为它对我有用:

CGRect screenRect = [[UIScreen mainScreen] bounds];    
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef context = UIGraphicsGetCurrentContext(); 
[[UIColor blackColor] set]; 
CGContextFillRect(context, screenRect);
[imageView.layer renderInContext:context];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save image
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), context);
[picker dismissModalViewControllerAnimated:YES];

Thank you @Rahul Vyas. I didn't try your suggestion, but I still appreciate your input. Ended up getting this issue resolved from help from the Apple Dev forums.

The answer to this problem is, as it worked for me:

CGRect screenRect = [[UIScreen mainScreen] bounds];    
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef context = UIGraphicsGetCurrentContext(); 
[[UIColor blackColor] set]; 
CGContextFillRect(context, screenRect);
[imageView.layer renderInContext:context];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save image
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), context);
[picker dismissModalViewControllerAnimated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文