【问题标题】:How to save a UIImage with a mask如何使用蒙版保存 UIImage
【发布时间】:2013-02-26 01:14:00
【问题描述】:

我有一个可以移动/缩放的UIImageView (self.imageForEditing)。在这个图像视图的顶部,我有一个带孔的覆盖层,它是静态的,不能移动。我只需要保存按下按钮时通过孔可见的底层图像的一部分。我目前的尝试:

- (IBAction)saveImage
{

    UIImage *image = self.imageForEditing.image;

    CGImageRef originalMask = [UIImage imageNamed:@"picOverlay"].CGImage;
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(originalMask),
                                        CGImageGetHeight(originalMask),
                                        CGImageGetBitsPerComponent(originalMask),
                                        CGImageGetBitsPerPixel(originalMask),
                                        CGImageGetBytesPerRow(originalMask),
                                        CGImageGetDataProvider(originalMask), nil, YES);

    CGImageRef maskedImageRef = CGImageCreateWithMask(image.CGImage, mask);

    UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef scale:image.scale orientation:image.imageOrientation];

    CGImageRelease(mask);
    CGImageRelease(maskedImageRef);

    UIImageView *test = [[UIImageView alloc] initWithImage:maskedImage];
    [self.view addSubview:test];
}

作为测试,我只是想将新创建的图像添加到屏幕的左上角。理论上它应该是一个小的圆形图像(通过覆盖可见的部分)。但我只是重新创建了整个图像。我究竟做错了什么?我如何解释self.imageForEditing 可以移动的事实?

【问题讨论】:

    标签: ios uiimageview uiimage masking cgimage


    【解决方案1】:

    CGImageCreateWithMask 返回与原始图像大小相同的图像。 这就是为什么你会得到带有蒙版的原始图像(我假设)。

    您可以应用蒙版,然后删除不可见的边框。使用这个问题的建议:iOS: How to trim an image to the useful parts (remove transparent border)

    找到图像不透明部分的边界并将其重新绘制成新图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      相关资源
      最近更新 更多