2012-03-08 48 views
1

简言之,应用旨在做到以下几点:UIGraphicsGetImageFromCurrentImageContext随意旋转图像?

  1. 用户将来自iPhone(通过ALAssets)内的照片进行选择。这部分没有问题。
  2. 所选照片将显示在另一个视图中,位于较小的矩形子视图内。
  3. 使用ALAsset缩略图时,图像在子视图中正确显示。但是,分辨率很差,所以我试图使用更高分辨率的图像。
  4. 当使用全分辨率图像,然后将其放入视图中时,任何人像照片将逆时针旋转90度。

代码明智的,这个它看起来像这样:

//ImageView is the UIImageView subview that will hold the selected photo image. 
//This code works ok, but I'd rather have a higher resolution photo. 
[imageView setClipsToBounds:YES]; 
[imageView setContentMode:UIViewContentModeScaleAspectFill]; 
[imageView setImage:[UIImage imageWithCGImage:[selectedPhoto thumbnail]]; 

现在,这是我尝试使用更高分辨率的图像的代码,但随后的ImageView内的图像旋转-90度:

[imageView setClipsToBounds:YES]; 
[imageView setContentMode:UIViewContentModeScaleAspectFill]; 

//HIGH RESOLUTION IMAGE 
ALAssetRepresentation *rep = [selectedPhoto defaultRepresentation]; 
CGImageRef iref = [rep fullResolutionImage]; 
if (iref) 
{ 
    UIImage *largeImage = [Utilities imageWithImage:[UIImage imageWithCGImage:iref] scaledToSize:CGSizeMake(CGImageGetWidth(iref)/4, CGImageGetHeight(iref)/4)]; 
    [imageView setImage:largeImage]; 
} 

而且imageWithImage:函数调用有如下:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

如前所述,通过iPhone摄像头拍摄的横向照片拍摄OK。但是,以纵向照相机拍摄的照片以某种方式旋转了-90度。

我该如何使每张照片(不论是以纵向还是横向拍摄)都能在我的ImageView上正确定向?

任何帮助,将不胜感激!谢谢!

+0

我在这里找到了答案! http://stackoverflow.com/questions/3648632/alassetrepresentation-fullresolutionimages-uiimageorientation-is-wrong 我现在这样做: ALAssetRepresentation *代表= [selectedPhoto defaultRepresentation] CGImageRef iref = [rep fullResolutionImage]; UIImage * largeImage = [[UIImage alloc] initWithCGImage:iref scale:4 orientation:rep.orientation]; PS:我还没有回答这个问题,因为没有足够的时间过去。 – kurisukun 2012-03-08 09:16:39

回答