views:

106

answers:

3

I took a sample code from the following URL: how to mask an image in order to mask an image.

The code is working perfectly on the iPhone simulator but works incorrectly on iPhone 4 simulator (that's when high-res images are loaded...)

Here is my code and the mask function:

- (void)someMethod {

    UIImage* image = [UIImage imageNamed:@"image.png"];  // [email protected] is loaded for high-res device

    UIImage* mask = [UIImage imageNamed:@"mask.png"];  // [email protected] is loaded for high-res device

    UIImage* maskedImage = [self maskImage:image withMask:mask];

    // ... Some code here displaying maskedImage

}

- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    UIImage* maskedImage = nil;

    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),               
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    maskedImage = [UIImage imageWithCGImage:masked];

    return maskedImage;
}

On the iPhone 4 simulator the [email protected] and [email protected] are loaded and then the resulted maskedImage is cropped.

Any idea what am I doing wrong?

A: 

My guess would be to not trust UIImage to deliver the highrez format to CGImageRefs etc. I think you will need to do it manually.

Chris Beeson
Thanks the comment Chris. Do you have any sample code that will do the masking work for both standard and high-res images?
Joshua
A: 

Use this method instead of above

  • (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    //UIImage *maskImage = [UIImage imageNamed:@"mask.png"]; CGImageRef maskImageRef = [maskImage CGImage];

    // create a bitmap graphics context the size of the image CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

    if (mainViewContentContext==NULL) return NULL;

    CGFloat ratio = 0;

    ratio = maskImage.size.width/ image.size.width;

    if(ratio * image.size.height < maskImage.size.height) { ratio = maskImage.size.height/ image.size.height; }

    CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}}; CGRect rect2 = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};

    CGContextClipToMask(mainViewContentContext, rect1, maskImageRef); CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);

    // Create CGImageRef of the main view bitmap content, and then // release that bitmap context CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext); CGContextRelease(mainViewContentContext);

    UIImage *theImage = [UIImage imageWithCGImage:newImage];

    CGImageRelease(newImage);

    // return the image return theImage; }

Ganesh
A: 

@ganesh, Hi, i i have masked a colored uiimage (a normal color image from photos album), with its image negative version. thanks to your chunk of code dude.

The result is, i have a negativeImage with the white areas of the negative image filled with colors from the Colored version.

now i wanted to achieve an effect that is just the opposite of what i have now. i.e. I wanted my colors to fill in the black areas of the negative image.

I went through apple developer docs, Bitmap Image Masking with MaskImage and i couldnt help myself achieve this.

Could someone help me ?? please...