如何从图像创建蒙版

时间:2013-10-15 10:07:37

标签: ios uiimageview uiimage masking

我在NSbundle中有一张图片。下面是图像

http://tinypic.com/view.php?pic=2093tvt&s=5

我想将此图像用作蒙版图像,并希望将其应用于任何图像,以便其他图像与上图像形状的形状相同。

所以这是一个重复的问题,但它对我不起作用..

如上所述,有人建议将图像放大,以便我做同样的事情。我通过代码将图像更改为灰度

- (UIImage *) convertToGreyscale:(UIImage *)i {

int kRed = 1;
int kGreen = 2;
int kBlue = 4;

int colors = kGreen;
int m_width = i.size.width;
int m_height = i.size.height;

uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

// now convert to grayscale
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y = 0; y < m_height; y++) {
    for(int x = 0; x < m_width; x++) {
        uint32_t rgbPixel=rgbImage[y*m_width+x];
        uint32_t sum=0,count=0;
        if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;}
        if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;}
        if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;}
        m_imageData[y*m_width+x]=sum/count;
    }
}
free(rgbImage);

// convert from a gray scale image back into a UIImage
uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);

// process the image back to rgb
for(int i = 0; i < m_height * m_width; i++) {
    result[i*4]=0;
    int val=m_imageData[i];
    result[i*4+1]=val;
    result[i*4+2]=val;
    result[i*4+3]=val;
}

// create a UIImage
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);

free(m_imageData);

// make sure the data will be released by giving it to an autoreleased NSData
[NSData dataWithBytesNoCopy:result length:m_width * m_height];

return resultUIImage;
}
下面的

是灰度图像

link1:

当我屏蔽图像然后在结果中得到低于结果 评论中给出的链接。我不能添加超过2个链接

LINK2:

我认为我还没有创建适当的遮罩图像

我试图掩盖下面的图像 http://tinypic.com/view.php?pic=10di24m&s=5

任何人都能说出制作面具图片的方法吗?

1 个答案:

答案 0 :(得分:0)

您正在使用的方法灰度似乎存在问题。它使圆圈外的颜色为黑色,但应该是白色以正确遮盖它。

这是我写给

的图像类别

<强>的UIImage + Mask.h

#import <UIKit/UIKit.h>

@interface UIImage (Mask)

- (UIImage*) maskImage:(UIImage *) image;
- (UIImage *)greyscaleImage;
@end

<强>的UIImage + Mask.m

@implementation UIImage (Mask)


- (UIImage*) maskImage:(UIImage *) image
{
    CGImageRef imageReference = image.CGImage;
    CGImageRef maskReference = self.CGImage;

    CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
                                             CGImageGetHeight(maskReference),
                                             CGImageGetBitsPerComponent(maskReference),
                                             CGImageGetBitsPerPixel(maskReference),
                                             CGImageGetBytesPerRow(maskReference),
                                             CGImageGetDataProvider(maskReference),
                                             NULL, // Decode is null
                                             YES // Should interpolate
                                             );

    CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask);
    CGImageRelease(imageMask);

    UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
    CGImageRelease(maskedReference);

    return maskedImage;
}

- (UIImage *)greyscaleImage {

    CGImageRef imgRef = self.CGImage;

    // Get the image width
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    // Create a Grayscale colour space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    /*
     * Create the context - based on orientation
     */
    CGContextRef context = nil;

    // If the orientation isn't UIImageOrientationUp then we'll need to rotate
    UIImageOrientation orient = self.imageOrientation;

    switch (orient) {
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
        case UIImageOrientationUpMirrored:

            context = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaNone);
            break;

        default:
            context = CGBitmapContextCreate(NULL, height, width, 8, 0, colorSpace, kCGImageAlphaNone);
            break;
    }

    CGColorSpaceRelease(colorSpace);

    /*
     * Create a context translation/rotation based on the orientation
     */
    switch(orient) {
        case UIImageOrientationUpMirrored:
            // 2 = 0th row is at the top, and 0th column is on the right - Flip Horizontal
            CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0));
            break;

        case UIImageOrientationDown:
            // 3 = 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height));
            break;

        case UIImageOrientationDownMirrored:
            // 4 = 0th row is at the bottom, and 0th column is on the left - Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height));
            break;

        case UIImageOrientationLeftMirrored:
            // 5 = 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width));
            break;

        case UIImageOrientationRight:
            // 6 = 0th row is on the right, and 0th column is the top - Rotate 90 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width));
            break;

        case UIImageOrientationRightMirrored:
            // 7 = 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0));
            break;

        case UIImageOrientationLeft:
            // 8 = 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0));
            break;

        case UIImageOrientationUp:
            // Nothing to do here
            break;
    }

    /*
     * Create the new image
     */
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, width, height));
    //CGContextFillEllipseInRect(context, );
    // Draw the image to the context
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);

    // Get a UIImage from the context
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *imageCopy =  [UIImage imageWithCGImage:imageRef];

    // Release
    CGImageRelease(imageRef);
    CGContextRelease(context);

    return imageCopy;
}

@end

以下是用法

UIImage *maskImage = [UIImage imageNamed:@"maskimage"];  // in your case the red dot image
UIImage *image = [UIImage imageNamed:@"image"];   // image to mask
maskImage = [maskImage greyscaleImage];
image = [maskImage maskImage:image];
self.imageView.image = image;