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

前端 未结 4 1610
被撕碎了的回忆
被撕碎了的回忆 2021-02-03 13:15

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?

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-03 13:32

    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

提交回复
热议问题