【问题标题】:Programmatically pasting image onto image以编程方式将图像粘贴到图像上
【发布时间】:2012-11-05 08:37:00
【问题描述】:

我有这个功能,可以截取我的应用程序上的一个小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];
}

【问题讨论】:

    标签: iphone objective-c ios uiimageview


    【解决方案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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多