1

在 ipad 上使用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 方法不给出图像。信息字典是

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

所以我在这个问题中使用了referenceURL 。我的代码是

-(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 获取的图像的方向。谢谢你

4

2 回答 2

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
于 2013-04-16T12:14:11.847 回答
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 ;
}
于 2013-04-17T06:04:57.303 回答