2010 Best Places to Work      Give us a call 888-856-2664
Get a Quote Today

Things a Chameleon Would Say

Web design

Objective C – Resizing and Masking an Image
By: Jacob Haskins 08/03/2010

A while back I ran into a situation where I needed to be able to display an image in different parts of an app with different sizes and masks. The solution I settled on was to save one image and resize/mask it as necessary. The code below is what I used to achieve that.

// Returns a resized image - (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {         UIGraphicsBeginImageContext(newSize);         [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];         UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();         UIGraphicsEndImageContext();         return newImage; } // Returns a masked image - (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {         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);         CGImageRelease(mask);         UIImage* retImage= [UIImage imageWithCGImage:masked];         CGImageRelease(masked);         return retImage; } ... UIImage *image = [UIImage imageNamed:@"myImage.png"]; UIImage *mask = [UIImage imageNamed:@"mask.png"]; UIImage *resizedImage = [self imageWithImage:image scaledToSize:CGSizeMake(76,92)]; UIImage *maskedImage = [self maskImage:resizedImage withMask:mask];

Image masking from [iPhone developer:tips];

One Response to “Objective C – Resizing and Masking an Image”

  1. BUDDAx2 December 6, 2010

    how to be if my image is smaller than mask (mask image is fullscreen size). How to set position of the image?

Leave a Reply




Have a Question? Need a Quote?

Drop us a line or give us a call at 888-856-2664

Your Name (required)
Your Email (required)
Phone (required)
Your Message

What Our Clients Are Saying

John,
Can I just tell you how impressed I am with your team?! This whole process has been so great and working with you guys has made it SOOOO much easier! Thank you for all your hard work and for convincing us to choose Accella.

SO THRILLED!!

Alycia White
Kubota

Recent Posts

Apple iOS 5 Tech Talks - What Accella Took Away

I had the opportunity to attend Apple's iOS 5 Tech Talks on January 23rd in...

Building Tablet Apps for the Enterprise: The Time Has Come

Having a background in rugged mobile devices and seeing the advantages that...