保存UIImage的缩略图会将图像侧向/上下翻转

时间:2015-07-15 02:11:11

标签: ios objective-c uiimage resize-crop

我正在保存UIImage的缩略图,以提高UICollectionView滚动的效果。所有图像直立保存,直到我尝试保存自下而上拍摄的全景照片(例如:高度 - 3000,宽度 - 1000)。这保存了颠倒。我拍摄了一张肖像并旋转90度,因为原来图像是侧面的。有没有办法用一行代码设置每种类型的图像,或者我是否必须捕捉每种类型的照片并相应地调整它?如果是这样怎么样?

 - (void) createThumbnail: (UIImage *) image{
     CGSize size = image.size;
     CGFloat imageHeight = size.height;
     CGFloat imageWidth = size.width;

     CGSize croppedSize;
     CGFloat ratio = 200.0;
     CGFloat offsetX = 0.0;
     CGFloat offsetY = 0.0;

     if (imageWidth > imageHeight) {
         offsetX = (imageHeight - imageWidth) / 2;
         croppedSize = CGSizeMake(imageHeight, imageHeight);
     } else {
         offsetY = (imageWidth - imageHeight) / 2;
         croppedSize = CGSizeMake(imageWidth, imageWidth);
     }


     CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
     CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);

     CGRect rect = CGRectMake(0.0, 0.0, ratio, ratio);

     UIGraphicsBeginImageContext(rect.size);
     [[UIImage imageWithCGImage:imageRef] drawInRect:rect];
     UIImage *thumbnail;



     if ((imageWidth > imageHeight) || (imageWidth == imageHeight)) {
         thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationUp];
     }else{
         thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationRight];
}

     UIGraphicsEndImageContext();
     return thumbnail;

2 个答案:

答案 0 :(得分:1)

我必须从didFinishPickingMediaWithInfo中选择的图片中获取图片方向,而不是裁剪的缩略图。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     choosenImage = info[UIImagePickerControllerOriginalImage];
     long orientation = choosenImage.imageOrientation;

     UIImage *thumbnail = [self createThumbnail:choosenImage withOrientation: orientation];
 }
 UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail;

if (orientation == UIImageOrientationUp) {
    thumbnail = UIGraphicsGetImageFromCurrentImageContext();
}else if (orientation == UIImageOrientationRight){
    thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationRight];
}else if (orientation == UIImageOrientationLeft){
    thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationLeft];
}else if (orientation == UIImageOrientationDown){
    thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationUpMirrored];
}

UIGraphicsEndImageContext();

答案 1 :(得分:0)

来自Photo Application的图片解决方案http://www.mathworks.com/help/javabuilder/web-deployment.html

ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:
                  [defaultRepresentation fullScreenImage]
                  scale:[defaultRepresentation scale]
                  orientation:0];