Search code examples
iphoneiosuiimagepickercontrolleruiinterfaceorientation

Image not returned by didFinishPickingMediaWithInfo: in ipad


On ipad using - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method donot give image. Info dictionary is

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

So i used referenceURL as asnwered in this question. My code for that is

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

I get Image this way, but image orientation is upside down.

Plese help either to get image directly from picker or chage the orintation of image that i get from url. Thank You


Solution

  • I got the solution for this orientation. While taking an image from ALAssetRepresentation *rep, I had used fullResolutionImage. Instead of that, I used fullScreenImage, and it solved the problem. Here is the code for that method.

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