0

我在 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;
}

下面是灰度图

链接1:

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

链接2:

我想我没有创建正确的蒙版图像

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

任何人都可以告诉制作蒙版图像的方法吗?

4

1 回答 1

0

There seems to be a problem with the method graysclae that you are using.It makes the color outside the circle as black, but it should be white to mask it properly.

Here is the image category that i have written to

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

Here is the usage

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;
于 2013-10-15T11:01:52.920 回答