How do I get a thumbnail or saveable path from UIImagePickerController to use for a UIImageView?

℡╲_俬逩灬. 提交于 2019-12-20 10:11:48

问题


Could somebody please explain or show some sample code of how I can use get a thumbnail to be put into a UIImageView after the user selects a photo with UIImagePickerController?

Let's say I want to set a thumbnail as the image of a table view cell. When the user presses the cell, the image picker is shown and the user selects an image that is already on their device. I also want to save the path to that thumbnail so that the next time the view is displayed, the proper thumbnail can be shown.

I am able to display an image picker and my delegate get's the chosen image correctly, but I want the path to the thumbnail, and I want to load the thumbnail using that path (i.e. I want to save the path). I've searched for hours today and haven't figured this out. Perhaps I'm not understanding ALAsset, or maybe it is something else, but I can't find any examples that are helping me. I have never used the image picker or ALAsset before now, so I'm completely new at this.


回答1:


//If you are reading this and look at the comments below and think ??WTF?? it is because I am editing my original Answer instead of posting two Answers in hopes that things will be cleaner. It is important to know that ALAssetsLibrary is an iOS 4.x thing.

The code below will function to take an asset-library URL and then make a UIImage out of the thumbnail representation. Though I use the asset-library url directly there is no reason that this same code couldn't begin by transforming a string representation into a NSURL in order to satisfy the imageURL assignment. Disclaimer: this code probably leaks or something worse, but it answers the original poster's question and is hopefully of value.

The code below is borrowing heavily on this Stack Overflow question that covers basically this same topic. In addition to the code here, I have included AssetsLibrary.framework and the ALAssetsLibrary typedefs referenced in the other question.

The whole trick is that you cannot reference the NSURL from an asset-library directly. I think (though I don't know) that it is somehow referencing a data store instead of a file so the data returned from the URL isn't straight NSData so you cannot use it the old way.

There is a UIImageView in the code that is called photo. Hopefully everything else is easy to figure out.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    NSLog(@"%@",imageURL);
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        CGImageRef iref = [myasset thumbnail];
        if (iref) {
            UIImage *theThumbnail = [UIImage imageWithCGImage:iref];
            [[self photo] setImage:theThumbnail];

        }
    };


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

    if(imageURL)
    {
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:imageURL 
                       resultBlock:resultblock
                      failureBlock:failureblock];
    }

    [self dismissModalViewControllerAnimated:YES];
}

If you don't want the thumbnail but do want the full photo, you will just change the code in the AssetForURLResult block to something like this:

       ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];

We can leave that exercize for the user.

Good luck, hope this helps clear things up for you.




回答2:


I've got it mostly figured out now. The difficulty I had was understanding Blocks, mostly the syntax of Blocks made it difficult to understand what they are and how they work.

In the delegate for the UIImagePickerController, when the user has selected an image, you can get the path to the picture in the assets library using:

NSURL *path = [info objectForKey:UIImagePickerControllerReferenceURL];

With that object, you can get the full size image or a thumbnail using the following code:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) {
        // Gets the full size image
        self.fullSizeImage = [UIImage imageWithCGImage:iref];
    }

    // Gets the thumbnail
    self.thumbnail = [UIImage imageWithCGImage:[myasset thumbnail]];
};

ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
    NSLog(@"in failureblock, got an error: %@",[myerror localizedDescription]);
};

ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib assetForURL:urlForImage resultBlock:resultblock failureBlock:failureblock];

This page really helped me understand Blocks: Blocks in iOS 4
See also display image from URL retrieved from ALAsset in iPhone




回答3:


Saving the URL can be problematic:

  1. at least in iOS 4.x, the URLs is not guaranteed to be unique. They can be reused if user deletes/add pictures, which is obviously common. So, the URL you kept can point to a different picture in the future!
  2. When the user updates to iOS 5, the URLs are no longer valid. UGH!!! Apple has rewritten Photo Library, with no backward compatibility.



回答4:


You can save the image you select from imagePicker into Documents Directory instaed of referencing it from the picker Here is the code you can use for saving image. Implement this method in the callback method invoked when user selects a photo from the picker

 NSFileManager   fileManager    = [NSFileManager defaultManager];

    NSArray*  output_PDF_paths  =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSString* output_PDF_documentsDirectory = [output_PDF_paths objectAtIndex:0];
    NSString* output_PDF_ABSOLUTE_PATH  = [output_PDF_documentsDirectory stringByAppendingPathComponent:@"ImageName.png"];  

    BOOL success_new = [UIImagePNGRepresentation(ProfilePhotoselected) writeToFile:previewPagePath atomically:YES];

//Here the "ProfilePhotoselected" is the image which you get from the picker as a argument in your callabck method



来源:https://stackoverflow.com/questions/4663019/how-do-i-get-a-thumbnail-or-saveable-path-from-uiimagepickercontroller-to-use-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!