我有以下功能,iOS 7及以前的功能。 XCode 5按预期工作。该函数采用图像和cropSize。图像是要裁剪为指定大小的图像,由CGSize cropSize定义。该功能的目的是将图像裁剪为特定大小,然后返回裁剪后的图像。
- (UIImage *) cropImage:(UIImage *)originalImage cropSize:(CGSize)cropSize
{
//calculate scale factor to go between cropframe and original image
float SF = originalImage.size.width / cropSize.width;
//find the centre x,y coordinates of image
float centreX = originalImage.size.width / 2;
float centreY = originalImage.size.height / 2;
//calculate crop parameters
float cropX = centreX - ((cropSize.width / 2) * SF);
float cropY = centreY - ((cropSize.height / 2) * SF);
CGRect cropRect = CGRectMake(cropX, cropY, (cropSize.width *SF), (cropSize.height * SF));
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], cropRect);
//keep orientation if landscape
UIImage *newImage;
if (originalImage.size.width > originalImage.size.height || originalImage.size.width == originalImage.size.height) {
newImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:originalImage.imageOrientation];
}
else
{
newImage = [UIImage imageWithCGImage:imageRef];
}
CGImageRelease(imageRef);
//Now want to scale down cropped image!
//want to multiply frames by 2 to get retina resolution
CGRect scaledImgRect = CGRectMake(0, 0, (cropSize.width * 2), (cropSize.height * 2));
UIGraphicsBeginImageContextWithOptions(scaledImgRect.size, NO, [UIScreen mainScreen].scale);
[newImage drawInRect:scaledImgRect];
UIImage *scaledNewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledNewImage;
}
问题是它一切正常,传递的UIImage是横向,图像按预期裁剪,但是如果传入的图像是纵向拍摄的,那么结果图像(裁剪的)结果scaledNewImage)在它的一侧旋转了90度,这是我不想要的。
仿佛正在处理肖像图像,就好像它是在风景中一样 - 因此该功能会在横向而不是纵向中裁剪出应该是纵向的图像。
如果裁剪区域是正方形,则不是那么明显,但是如果要裁剪的区域是横向矩形,则沿着纵向的长度而不是宽度裁剪它。希望我有意义!
此问题在iOS 7&之前未发生过。 XCode 5 ..所以我不确定究竟发生了什么变化。任何帮助表示感谢,谢谢。
答案 0 :(得分:16)
借助这里的答案解决了这个问题:https://stackoverflow.com/a/14712184/521653
- (UIImage *) cropImage:(UIImage *)originalImage cropSize:(CGSize)cropSize
{
NSLog(@"original image orientation:%d",originalImage.imageOrientation);
//calculate scale factor to go between cropframe and original image
float SF = originalImage.size.width / cropSize.width;
//find the centre x,y coordinates of image
float centreX = originalImage.size.width / 2;
float centreY = originalImage.size.height / 2;
//calculate crop parameters
float cropX = centreX - ((cropSize.width / 2) * SF);
float cropY = centreY - ((cropSize.height / 2) * SF);
CGRect cropRect = CGRectMake(cropX, cropY, (cropSize.width *SF), (cropSize.height * SF));
CGAffineTransform rectTransform;
switch (originalImage.imageOrientation)
{
case UIImageOrientationLeft:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI_2), 0, -originalImage.size.height);
break;
case UIImageOrientationRight:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI_2), -originalImage.size.width, 0);
break;
case UIImageOrientationDown:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI), -originalImage.size.width, -originalImage.size.height);
break;
default:
rectTransform = CGAffineTransformIdentity;
};
rectTransform = CGAffineTransformScale(rectTransform, originalImage.scale, originalImage.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectApplyAffineTransform(cropRect, rectTransform));
UIImage *result = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation];
CGImageRelease(imageRef);
//return result;
//Now want to scale down cropped image!
//want to multiply frames by 2 to get retina resolution
CGRect scaledImgRect = CGRectMake(0, 0, (cropSize.width * 2), (cropSize.height * 2));
UIGraphicsBeginImageContextWithOptions(scaledImgRect.size, NO, [UIScreen mainScreen].scale);
[result drawInRect:scaledImgRect];
UIImage *scaledNewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledNewImage;
}
那是那里更新的方法。我实现了链接到我的方法的答案中的代码,它解决了问题。奇怪的是我在iOS 7之前没有这些!