1

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info メソッドを使用 するiPadでは、画像を提供しないでください。情報辞書は

> 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

このオリエンテーションの解決策を得ました。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 に答える
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 に答える