I am having a problem in cropping an image .
My flow is :
1)Crop an image to 1:1 (for example , if there is any image of size 665 X 985 px then crop it to 665 X 665 px ).
2)resize it to 612 X 612 px same like instagram .
For that the code i use is :
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *d=UIImagePNGRepresentation(image);
CGFloat pic_crop;
NSLog(@"Size of Image(bytes):%lu",(unsigned long)[d length]);
NSLog(@" original Image Size :%.2f \n",(float)d.length/1024.0f/1024.0f);
NSLog(@"Original height --->%f",image.size.height);
NSLog(@"Original width --->%f",image.size.width);
if (image.size.height > image.size.width) {
pic_crop=image.size.width;
NSLog(@"%f",image.size.width);
}
else{
pic_crop=image.size.height;
NSLog(@"%f",image.size.height);
}
then i crop it , if height is less then crop it to height size or width size !
_previewImage.frame = CGRectMake(
_previewImage.frame.origin.x,
_previewImage.frame.origin.y, pic_crop, pic_crop);
UIImage *croppedImage = [image croppedImage:CGRectMake((image.size.width -_previewImage.frame.size.width)/2, (image.size.height -_previewImage.frame.size.height)/2, _previewImage.frame.size.width, _previewImage.frame.size.height)];
NSLog(@"crop height --->%f",croppedImage.size.height);
NSLog(@"crop width --->%f",croppedImage.size.width);
NSData *d2=UIImagePNGRepresentation(croppedImage);
NSLog(@"Size of Image(bytes):%lu",(unsigned long)[d2 length]);
NSLog(@" After compression :%.2f \n",(float)d2.length/1024.0f/1024.0f);
Then i resize it to 612 X 612 px ,
CGSize newSize;
newSize = CGSizeMake(612 , 612);
UIGraphicsBeginImageContext( newSize );
[croppedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This is how i perform this thing , it runs completely fine in simulator but not in device !
It shrinks picture a lot in device ! Please help me out ! Thanks in Advance .
Use
UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
to create an image context that is adapted to either retina or non-retina depending on the device.