Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. CGRect ClippedRect= CGRectMake(0, 150, 320.0, 230.0);//example numbers
  2. CGImageRef imageRef = CGImageCreateWithImageInRect([OriginalUIImage CGImage], ClippedRect);
  3. UIImage *resultUIImage=[[UIImage alloc]initWithCGImage:imageRef];
  4.  
  5. - (UIImage*) maskImage:(UIImage *)image {
  6.  
  7. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  8.  
  9. UIImage *maskImage = [UIImage imageNamed:@"mask.png"];
  10. CGImageRef maskImageRef = [maskImage CGImage];
  11.  
  12. // create a bitmap graphics context the size of the image
  13. CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
  14.  
  15.  
  16. if (mainViewContentContext==NULL)
  17. return NULL;
  18.  
  19. CGFloat ratio = 0;
  20.  
  21. ratio = maskImage.size.width/ image.size.width;
  22.  
  23. if(ratio * image.size.height < maskImage.size.height) {
  24. ratio = maskImage.size.height/ image.size.height;
  25. }
  26.  
  27. CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
  28. 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}};
  29.  
  30.  
  31. CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
  32. CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
  33.  
  34.  
  35. // Create CGImageRef of the main view bitmap content, and then
  36. // release that bitmap context
  37. CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
  38. CGContextRelease(mainViewContentContext);
  39.  
  40. UIImage *theImage = [UIImage imageWithCGImage:newImage];
  41.  
  42. CGImageRelease(newImage);
  43.  
  44. // return the image
  45. return theImage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement