iOS how to show images with asset library path without binding them to instance variables

645 views Asked by At

I can get image with asset library path and show it on view via AssetLibrary, but the image is assigned to an instance variable of the controller. Now, I want to show multiple images on view and therefore I shouldn't bind these images to instance variables because I don't know how many of them will be included. Any ideas? Basically what I'm trying to do here is I want users to be able to select images from Photo Library and show all of them.

1

There are 1 answers

0
Alex Jin On

The code below may be help you.

-(void) putDataFromAssertUrl:(NSURL*)url ToImageView:(UIImageView*) iv // url is an assert url, can't be directly used.
{
    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
    [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        Byte *buffer = (Byte*)malloc(rep.size);
        NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
        NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
        NSLog(@"length : %lu", (unsigned long)data.length);
        iv.image = [UIImage imageWithData:data];
        [iv setNeedsDisplay];
    } failureBlock:^(NSError *err) {
        NSLog(@"Error: %@",[err localizedDescription]);
    }];
}