【问题标题】:How do I "combine" two UIImageViews into one image? Like Photoshop Layer merging?如何将两个 UIImageViews “组合”成一张图像?喜欢 Photoshop 图层合并?
【发布时间】:2012-09-26 12:49:48
【问题描述】:

我有一个绘图应用程序,其中有一个 UIImageView 作为“绘图层”。在它下面还有另一个 UIImageView,它是“图像层”,包含您正在绘制的图像。我喜欢这种分离。但是,我希望用户能够“保存并通过电子邮件发送”他们在图像顶部制作的绘图作为一个统一的图像。我该怎么做?

【问题讨论】:

    标签: ios uiimageview drawing


    【解决方案1】:

    您的UIImageView 实例必须是UIView 层次结构的一部分,因此您需要做的就是将包含UIView 的顶部绘制到上下文中

    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
    [container.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    

    或者如果这给您带来麻烦,请连续将两个图像绘制到上下文中

    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
    [image1.layer renderInContext:UIGraphicsGetCurrentContext()];
    [image2.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    

    您可以从那里写入您选择的数据

    NSData *data = UIImagePNGRepresentation(fimage);
    

    将该数据传递到 MailComposer 设置中。

    【讨论】:

    • 整天都在寻找那个 renderInContext 方法。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2022-01-19
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多