如何使图像始终显示正确的方向?

时间:2016-06-24 08:26:35

标签: ios iphone image-rotation landscape-portrait

在landscapeLeft和landscapeRight方向从相机拍摄图像后,以横向模式保存图像。附上结果图像。

以下是我的代码以及我尝试过的所有内容:

我正在使用PHPhotoLibrary将图像保存在自定义相册中。

 // Take Image Button Method
 - (void)snapButtonPressed:(UIButton *)button
  {
  [self.camera capture:^(LLSimpleCamera *camera, UIImage *image,           NSDictionary *metadata, NSError *error)
        {
        if(!error)
          {

        NSString * info = [NSString stringWithFormat:@"Size: %@  -  Orientation: %ld", NSStringFromCGSize(image.size), (long)image.imageOrientation];
        NSLog(@"IMAGE Meta Data:%@",info);

        [CustomAlbum addNewAssetWithImage:image toAlbum:[CustomAlbum getMyAlbumWithName:CSAlbum] onSuccess:^(NSString *ImageId)
        {
            NSLog(@"IMAGE ID:%@",ImageId);
            recentImg = ImageId;
        }
           onError:^(NSError *error)
        {
            NSLog(@"probelm in saving image");
        }];
    }
    else
    {
        NSLog(@"An error has occured: %@", error);
    }
 }
      exactSeenImage:YES];
}
  • 列表项

enter image description here

1 个答案:

答案 0 :(得分:0)

我使用此方法实现修复图像旋转,同时从相机捕获图像。 我认为这对你有用。

- (UIImage *) fixrotation:(UIImage *)image {


if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;

switch (image.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, image.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (image.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                         CGImageGetBitsPerComponent(image.CGImage), 0,
                                         CGImageGetColorSpace(image.CGImage),
                                         CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
        break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;

 }