简体   繁体   中英

Saving thumbnail of UIImage flips the image sideways/upside down

I am saving a thumbnail of a UIImage to improve performance of my UICollectionView scrolling. All images saved upright until I tried saving a panoramic shot that was shot bottom-up (Ex: height - 3000, width - 1000). This saved upside down. I catch a portrait shot and rotate 90 because originally the image is sideways. Is there a way to set every type of image upright with a line of code, or do I have to catch every type of photo and adjust it accordingly? And if so how?

 - (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;

I had to get the image orientation from the chosen Image in didFinishPickingMediaWithInfo instead of the cropped thumbnail.

 - (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();

Solution for Images coming from Photo Application : 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM