首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImagePNGRepresentation和蒙面图像

UIImagePNGRepresentation和蒙面图像
EN

Stack Overflow用户
提问于 2010-11-15 21:51:38
回答 2查看 1.8K关注 0票数 3
  1. I使用iphone博客的函数格式创建了一个蒙面图像: UIImage *imgToSave = [self maskImage:UIImage:@“pic.jpg”withMask:UIImage:@“sd-face-mask.png”];
  2. 在UIImageView中很好看 UIImageView *imgView = [UIImageView alloc initWithImage:imgToSave];imgView.center = CGPointMake(160.0f,140.0f);self.view addSubview:imgView;
  3. UIImagePNGRepresentation保存到磁盘: [UIImagePNGRepresentation(imgToSave) writeToFile:self findUniqueSavePath原子化:YES];

UIImagePNGRepresentation返回一个看起来不同的图像的NSData。

输出为逆像掩模。应用程序中删除的区域现在可以在文件中看到。应用程序中可见的区域现在被删除了。能见度是相对于

我的面具是用来除去除了照片中的面部区域以外的所有东西。UIImage在应用程序中看起来很正确,但是在我将它保存到磁盘上之后,文件看起来就相反了。这张脸被移除了,但是其他的东西都在这里。

如果你能帮忙,请告诉我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-12 22:38:41

我也有同样的问题,当我保存文件时,这是一种方式,但是内存中返回的图像正好相反。

罪魁祸首&解决方案是UIImagePNGRepresentation()。在将其保存到磁盘之前,它修复了应用程序中的映像,因此我只是插入了该函数,作为创建蒙面映像并返回该功能的最后一步。

这可能不是最优雅的解决方案,但它有效。我从我的应用程序中复制了一些代码,并压缩了它,不确定下面的代码是否正常工作,但如果不是,它就关闭了。也许只是打了几个字。

好好享受吧。:)

代码语言:javascript
复制
// MyImageHelperObj.h

@interface MyImageHelperObj : NSObject

+ (UIImage *) createGrayScaleImage:(UIImage*)originalImage;
+ (UIImage *) createMaskedImageWithSize:(CGSize)newSize sourceImage:(UIImage *)sourceImage maskImage:(UIImage *)maskImage;

@end





// MyImageHelperObj.m

#import <QuartzCore/QuartzCore.h>
#import "MyImageHelperObj.h"


@implementation MyImageHelperObj


+ (UIImage *) createMaskedImageWithSize:(CGSize)newSize sourceImage:(UIImage *)sourceImage maskImage:(UIImage *)maskImage;
{
    // create image size rect
    CGRect newRect = CGRectZero;
    newRect.size = newSize;

    // draw source image
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0f);
    [sourceImage drawInRect:newRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    // draw mask image
    [maskImage drawInRect:newRect blendMode:kCGBlendModeNormal alpha:1.0f];
    maskImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // create grayscale version of mask image to make the "image mask"
    UIImage *grayScaleMaskImage = [MyImageHelperObj createGrayScaleImage:maskImage];
    CGFloat width = CGImageGetWidth(grayScaleMaskImage.CGImage);
    CGFloat height = CGImageGetHeight(grayScaleMaskImage.CGImage);
    CGFloat bitsPerPixel = CGImageGetBitsPerPixel(grayScaleMaskImage.CGImage);
    CGFloat bytesPerRow = CGImageGetBytesPerRow(grayScaleMaskImage.CGImage);
    CGDataProviderRef providerRef = CGImageGetDataProvider(grayScaleMaskImage.CGImage);
    CGImageRef imageMask = CGImageMaskCreate(width, height, 8, bitsPerPixel, bytesPerRow, providerRef, NULL, false);

    CGImageRef maskedImage = CGImageCreateWithMask(newImage.CGImage, imageMask);
    CGImageRelease(imageMask);
    newImage = [UIImage imageWithCGImage:maskedImage];
    CGImageRelease(maskedImage);
    return [UIImage imageWithData:UIImagePNGRepresentation(newImage)];
}

+ (UIImage *) createGrayScaleImage:(UIImage*)originalImage;
{
    //create gray device colorspace.
    CGColorSpaceRef space = CGColorSpaceCreateDeviceGray();
    //create 8-bit bimap context without alpha channel.
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL, originalImage.size.width, originalImage.size.height, 8, 0, space, kCGImageAlphaNone);
    CGColorSpaceRelease(space);
    //Draw image.
    CGRect bounds = CGRectMake(0.0, 0.0, originalImage.size.width, originalImage.size.height);
    CGContextDrawImage(bitmapContext, bounds, originalImage.CGImage);
    //Get image from bimap context.
    CGImageRef grayScaleImage = CGBitmapContextCreateImage(bitmapContext);
    CGContextRelease(bitmapContext);
    //image is inverted. UIImage inverts orientation while converting CGImage to UIImage.
    UIImage* image = [UIImage imageWithCGImage:grayScaleImage];
    CGImageRelease(grayScaleImage);
    return image;
}

@end
票数 0
EN

Stack Overflow用户

发布于 2010-11-16 05:20:51

在石英中,你可以通过一个图像掩码(黑色通过和白色块),或正常图像(白色通过和黑色块),这是相反的。似乎出于某种原因,保存是将图像掩码作为一个正常的图像来掩蔽。一个想法是渲染到位图上下文,然后创建一个图像来保存。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4189166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档