问题描述
可能重复:保存两个重叠的 UIImage
在 UIScrollView 中,我添加了我的 UIImageView,它上面有一个 框架层.我想在所有编辑后将最终图像保存在屏幕上.对上述问题的回答可以解决问题,但在绘制框架时,会导致图像质量下降,因此我正在寻找一种可以使图像保持良好分辨率的解决方案.请帮我解决这个问题,提前致谢!
In a UIScrollView, i've added my UIImageView and there's a frame Layer above it. I want to save the final Image on screen after all editing. Answer given to the above mentioned question does the work but as it's drawing the frame, results in degradation of quality of my image , So i'm searching for a solution which keeps my image in good resolution.Please help me out of this, thanks in advance !
推荐答案
试试这个代码:
//merge two images for this code
UIImage *bottomImage =imgview.image; //background image ////1st image
UIImage *image = imgProfile.image; //foreground image///2nd image
CGSize newSize = CGSizeMake(270, 330); // set your image rect
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];///1st image set frame
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(81,218,97,78) blendMode:kCGBlendModeNormal alpha:1]; //2nd image set frame on bottom image with alpha value
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(newImage);
[imageData writeToFile:savedImagePath atomically:NO];
您可以在应用程序的文档目录中看到新创建的图像.
You can see your newly created image in your application's document directory.
这篇关于将两张重叠的图像合二为一,iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!