0

我正在保存 a 的缩略图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;
4

2 回答 2

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();
于 2015-07-15T17:32:04.217 回答
0

来自照片应用程序的图像的解决方案https ://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetRepresentation_Class/

ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:
                  [defaultRepresentation fullScreenImage]
                  scale:[defaultRepresentation scale]
                  orientation:0];
于 2015-07-15T02:28:52.130 回答