我在裁剪图像方面遇到了问题。
我的流程是:
1)将图像裁剪为1:1(例如,如果有任何尺寸为665 X 985像素的图像,则将其裁剪为665 X 665像素。)
2)将其大小调整为612 X 612像素,与Instagram相同。
为此我使用的代码是:
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);
}
然后我裁剪它,如果高度较小,则将其裁剪为高度尺寸或宽度尺寸!
_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);
然后我将其调整为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();
这就是我执行此操作的方式,它在模拟器中运行完全正常但在设备中没有运行!
它在设备中缩小了很多图片!请帮帮我!在此先感谢。
答案 0 :(得分:1)
使用UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
创建适合视网膜或非视网膜的图像上下文,具体取决于设备。