Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

Thread: Dynamic Mask
View Single Post
Old 07-06-2011, 08:52 PM   #14 (permalink)
mdejong1024
Registered Member
 
Join Date: Dec 2010
Posts: 7
mdejong1024 is on a distinguished road
Default Util method I use for this

I recently needed to figure out how to implement a solution like this. Here is the code I use:

Code:
// This cropping method will render the imageToCrop into a new graphics context
// cropped by the greyscale image defined by cropToImage. The result is returned
// as a RGBA image. The result of this operation is an image where the greyscale
// pixel value is converted into the alpha channel for the pixels.

- (UIImage*)imageByCropping:(UIImage*)imageToCrop
                cropToImage:(UIImage*)cropToImage
{
  // create a context to do our clipping in

  CGImageRef imageToCropRef = imageToCrop.CGImage;
  CGImageRef cropToImageRef = cropToImage.CGImage;
  
  size_t width = CGImageGetWidth(imageToCropRef);
  size_t height = CGImageGetHeight(imageToCropRef);
  CGSize size = CGSizeMake(width, height);
  
  UIGraphicsBeginImageContext(size);
  CGContextRef currentContext = UIGraphicsGetCurrentContext();
  
  // Flip coordinate system

  CGContextTranslateCTM(currentContext, 0.0, size.height);
  CGContextScaleCTM(currentContext, 1.0, -1.0);
  
  // Create a new image that is the size of the original image.
    
  CGRect clippedRect = CGRectMake(0, 0, size.width, size.height);
  
  CGContextClipToMask( currentContext, clippedRect, cropToImageRef);
    
  CGRect drawRect = CGRectMake(0,
                               0,
                               imageToCrop.size.width,
                               imageToCrop.size.height);
  
  // draw the image to our clipped context using our offset rect
  CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
  
  // pull the image from our cropped context
  UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
  
  // pop the context to get back to the default
  UIGraphicsEndImageContext();
  
  // Note: this is autoreleased
  return cropped;
}
The trick is to generate a mask image that is a grayscale image, then the white parts are transparent and the black parts can't be seen. Anything in between becomes the alpha channel for the resulting image.
mdejong1024 is offline   Reply With Quote
 

» Advertisements
» Online Users: 378
15 members and 363 guests
Absentia, alisafl22, bbbsaha, DaveDee, erdinc27, fredidf, Hercule, heshiming, Johnsyfared, milanalina, prbiztech, QuantumDoja, roof44, waghman, wserver
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,603
Threads: 94,084
Posts: 402,782
Top Poster: BrianSlick (7,990)
Welcome to our newest member, alisafl22
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:58 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.