我有这个功能,可以对我的应用程序上的一个小UIView
进行屏幕截图,我的问题是我想向该图像添加位于我的图像文件夹中的背景。我想要添加的背景没有显示在当前 View 上,我只想在保存图像之前以编程方式将其粘贴到屏幕截图图像后面。可能吗?
(我不想创建一个带有背景的新 View ,也不想添加屏幕截图图像,也不想再次拍摄屏幕截图。这太多了)
假设这是已拍摄的图像:
______
/ \
/ \
\ /
\______/
这是带有图像的背景图像:
_________________
| |
| ______ |
| / \ |
| / \ |
| \ / |
| \______/ |
| |
|_________________|
这是我的代码:
- (void)takeScreenShotAndSaveIt:(CGPoint)center
{
// IMPORTANT: using weak link on UIKit
if(UIGraphicsBeginImageContextWithOptions != NULL)
{
UIGraphicsBeginImageContextWithOptions(smallview.frame.size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(smallview.frame.size);
}
[smallview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save image to iPhone documents directory
NSData * imgData = [[NSData alloc] initWithData:UIImagePNGRepresentation(viewImage)];
NSString * filename = [NSString stringWithFormat:@"MeasureScreenShot.png"];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
[imgData writeToFile:path atomically:YES];
}
最佳答案
如果我正确理解你想要做什么,请尝试以下操作:
- (void)takeScreenShotAndSaveIt:(CGPoint)center
{
...
[backgroundImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[smallview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
...
其中backgroundImageView
是包含背景图像的UIImageView。您只需在同一上下文中渲染两个 View 即可。如果您更喜欢将背景处理为图像而不是 View ,则可以使用:
[bckImage drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
关于iphone - 以编程方式将图像粘贴到图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13228491/