1

關於ipad使用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法不給圖像。信息詞典didFinishPickingMediaWithInfo返回的圖片:在ipad上

> dic = { 
>  UIImagePickerControllerMediaType = "public.image"; 
>  UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=1D8618CC-CDF1-478C-A36D-455A66501A02&ext=JPG"; 
> } 

所以我用referenceURL在this問題asnwered。 我的代碼是

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    editPhoto=YES; 
    [self dismissModalViewControllerAnimated:YES]; 
     NSLog(@"dic = %@",info); 
     defaultImageView.image = [info valueForKey:@"UIImagePickerControllerOriginalImage"]; 
     if(defaultImageView.image == nil) 
     { 
      NSLog(@"image from assets"); 
      NSURL *imageSource = [info objectForKey:@"UIImagePickerControllerReferenceURL"]; 
      [self findLargeImage:imageSource]; 
     } 



     NSLog(@"image picked in block"); 


} 

-(UIImage*)findLargeImage :(NSURL*)path 
{ 
    __block UIImage * largeimage=nil; 
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
     ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
     CGImageRef iref = [rep fullResolutionImage]; 
     if (iref) { 

      largeimage =[UIImage imageWithCGImage:iref]; 
      defaultImageView.image = [ImageOreintation fixOrientation:largeimage]; 
      int width = defaultImageView.image.size.width; 
      int height = defaultImageView.image.size.height; 
      int frameWidth = (158.0/height) * width; 
      if(frameWidth>212) 
      { 
       defaultImageView.frame = CGRectMake(26, 10, 212, 158); 
      } 
      else 
      { 
       defaultImageView.frame = CGRectMake(132-frameWidth/2, 10, frameWidth, 158); 
      } 

      NSLog(@"in find large image3"); 
     } 
    }; 
    NSLog(@"in find large image4"); 
    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
    { 
     NSLog(@"booya, cant get image - %@",[myerror localizedDescription]); 
    }; 

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 

    [assetslibrary assetForURL:path resultBlock:resultblock failureBlock:failureblock]; 

    return largeimage ; 
} 

我得到的圖像這樣的,但形象定位是上下顛倒。

請幫忙或者直接從選取器中獲取圖像,或者改變我從url獲取圖像的方式。 謝謝

+0

http://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more - >有一些關於如何獲得UIImage對象的提示。 「如何調整圖像大小」的方法2也修復了方向。 – Marc

回答

0

我得到這個方位解決方案。從ALAssetRepresentation * rep拍攝圖像時,我使用了fullResolutionImage。取而代之的是,我使用了fullScreenImage,並解決了這個問題。 這是該方法的代碼。

-(UIImage*)findLargeImage :(NSURL*)path 
{ 
    __block UIImage * largeimage=nil; 
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
     ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
     largeimage = [UIImage imageWithCGImage:[rep fullScreenImage] scale:[rep scale] orientation:0]; 

     } 
    }; 
    NSLog(@"in find large image4"); 
    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
    { 
     NSLog(@"cant get image - %@",[myerror localizedDescription]); 
    }; 

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 

    [assetslibrary assetForURL:path resultBlock:resultblock failureBlock:failureblock]; 

    return largeimage ; 
} 
0

你可以嘗試使用這一類的固定圖像的方向。

頭文件

#import <UIKit/UIKit.h> 

@interface UIImage (Orientation) 

- (UIImage *)fixOrientation; 

@end 

實現文件

#import "UIImage+Orientation.h" 

@implementation UIImage (Orientation) 

- (UIImage *)fixOrientation { 

    if (self.imageOrientation == UIImageOrientationUp) return self; 

    CGAffineTransform transform = CGAffineTransformIdentity; 

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

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

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

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

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

    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, 
             CGImageGetBitsPerComponent(self.CGImage), 0, 
             CGImageGetColorSpace(self.CGImage), 
             CGImageGetBitmapInfo(self.CGImage)); 
    CGContextConcatCTM(ctx, transform); 
    switch (self.imageOrientation) { 
     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 

      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); 
      break; 

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

    CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
    UIImage *img = [UIImage imageWithCGImage:cgimg]; 
    CGContextRelease(ctx); 
    CGImageRelease(cgimg); 
    return img; 
} 

@end 
+0

它只給予方向。但形象是倒掛的。我也使用過這個代碼,但沒有工作。如果僅僅是返回,如果(self.imageOrientation == UIImageOrientationUp)返回self; – Durgaprasad