0

I am googling from last few hour and get solution for crop an Image, But I am confused and wants to know what is the difference between these two?

Problem : I wants to crop a rectangle image from inside an Image, I already detected the boundary.enter image description here

First :

 CGRect rect = CGRectMake(0,0,320, 460);

// Create bitmap image from original image data,
// using rectangle to specify desired crop area

CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], rect);
UIImage *imgs = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Second :

CGRect rect = [backView bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

[backView.layer renderInContext:context];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
Milind Thakkar
  • 980
  • 2
  • 13
  • 20
QueueOverFlow
  • 1,336
  • 5
  • 27
  • 48
  • have a look on these links http://stackoverflow.com/questions/9578821/iphonehow-to-crop-image-in-ios5 and http://stackoverflow.com/questions/7950719/how-to-crop-the-image-in-iphone – Venk Oct 23 '12 at 10:25

1 Answers1

2

The second method is more general, in that your backView needs not to be an UIImageView, like in the first case.

In other words, CGImageCreateWithImageInRect requires you to have a CGImage to start with:

CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], rect);

on the other hand with renderInContext you can render any view into an image:

[backView.layer renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
sergio
  • 68,819
  • 11
  • 102
  • 123