0

我有这个功能,可以截取我的应用程序上的一个小屏幕截图UIView,我的问题是我想在这个图像中添加一个位于我的图像文件夹中的背景。我要添加的背景未显示在当前视图上,我只想在保存图像之前以编程方式将其粘贴到屏幕截图图像后面。可能吗?

(我不想创建一个带有背景的新视图,而不是添加屏幕截图图像,而不是再次拍摄屏幕截图。太多了)

可以说这是已拍摄的图像:

  ______
 /      \
/        \
\        /
 \______/

这是与图像一起的背景图像:

 _________________
|                 |
|     ______      |
|    /      \     |
|   /        \    |
|   \        /    |
|    \______/     |
|                 |
|_________________|

这是我的代码:

- (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];
}
4

1 回答 1

1

如果我正确理解您要执行的操作,请尝试以下操作:

- (void)takeScreenShotAndSaveIt:(CGPoint)center
{
  ...
  [backgroundImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
  [smallview.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  ...

backgroundImageView包含背景图像的 UIImageView在哪里。您只需在同一上下文中渲染两个视图。如果您更喜欢将背景作为图像而不是视图来处理,您可以使用:

[bckImage drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
于 2012-11-05T08:42:49.083 回答