代码之家  ›  专栏  ›  技术社区  ›  Alex L

UIImagePNGRepresentation和蒙面图像

  •  3
  • Alex L  · 技术社区  · 14 年前
    1. 创建蒙面图像 使用 function 创建一个iphone博客:

      UIImage*imgToSave=[自拍图像:[UIImage imageNamed:@“pic.jpg”]带掩码:[UIImage imageNamed:@“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)写入文件:[self findUniqueSavePath]原子:是];

    UIImagePNGRepresentation格式 返回看起来不同的图像的NSData。

    输出为逆图像掩模。 应用程序中删除的区域现在在文件中可见。 应用程序中可见的区域现在已被删除。可见性是 相反的 .

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

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

    2 回复  |  直到 13 年前
        1
  •  0
  •   Travis Weerts    11 年前

    我也有同样的问题,当我保存文件时,它是一种方式,但返回到内存中的图像却是完全相反的。

    罪魁祸首解决方案是UIImagePNGRepresentation()。它在保存到磁盘之前修复了应用程序内的映像,所以我在创建屏蔽映像并返回该映像的最后一步插入了该函数。

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

    享受吧。:)

    // 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
    
        2
  •  3
  •   fhj    14 年前

    在石英中,您可以通过图像蒙版(黑色通孔和白色块)或相反的正常图像(白色通孔和黑色块)来进行蒙版。出于某种原因,saving将图像蒙版视为要蒙版的普通图像。一个想法是渲染到位图上下文,然后创建一个要从中保存的图像。

    推荐文章