Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使图像始终显示正确的方向?_Ios_Iphone_Image Rotation_Landscape Portrait - Fatal编程技术网

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

Ios 如何使图像始终显示正确的方向?,ios,iphone,image-rotation,landscape-portrait,Ios,Iphone,Image Rotation,Landscape Portrait,在以landscapeLeft和landscapeRight方向从相机拍摄图像后,图像以横向模式保存。结果图像已附加 以下是我的代码和我尝试过的所有东西: 我正在使用PHPhotoLibrary在自定义相册中保存图像 // Take Image Button Method - (void)snapButtonPressed:(UIButton *)button { [self.camera capture:^(LLSimpleCamera *camera, UIImage *imag

在以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];
}
  • 列表项

我已经使用此方法实现了固定图像旋转,同时从相机捕获图像。 我认为这对你很有用

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

 }

您需要保留原始图片中的exif信息,但我还没有找到有关PHPhotoLibrary的任何信息。既然您知道问题出在exif旋转信息的丢失上,也许您会更幸运。@mukesh您要检查我的代码吗?