Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

iphone - ALAsset Photo Library Image Performance Improvements When Its Loading Slow

Hi i've got a problem with display an image on my scrollView.

At first i create new UIImageView with asset url:

-(void) findLargeImage:(NSNumber*) arrayIndex
{
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep;
    if([myasset defaultRepresentation] == nil) {
        return;
    } else {
        rep = [myasset defaultRepresentation];
    }
    CGImageRef iref = [rep fullResolutionImage];
    itemToAdd = [[UIImageView alloc] initWithFrame:CGRectMake([arrayIndex intValue]*320, 0, 320, 320)];
    itemToAdd.image = [UIImage imageWithCGImage:iref];
    [self.scrollView addSubview:itemToAdd];
};
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"Cant get image - %@",[myerror localizedDescription]);
};
NSURL *asseturl = [NSURL URLWithString:[self.photoPath objectAtIndex:[arrayIndex intValue] ]];
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:asseturl 
               resultBlock:resultblock
              failureBlock:failureblock];

}

Where itemToAdd is a UIImageView define in interface:

__block UIImageView *itemToAdd;

And scrollView define as a property:

@property (nonatomic, strong) __block UIScrollView *scrollView;

Then in my viewWillAppear i do this:

- (void) viewWillAppear:(BOOL)animated {
    self.scrollView.delegate = self;
    [self findLargeImage:self.actualPhotoIndex];
    [self.view addSubview:self.scrollView];
}

But image doesnt appear, should i refresh self.view after add image to scrollView, or should do something else?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ALAssetsLibrary block will execute in separate thread. So I suggest to do the UI related stuffs in main thread.

To do this either use dispatch_sync(dispatch_get_main_queue() or performSelectorOnMainThread

Some Important Notes:

  1. Use AlAsset aspectRatioThumbnail instead of fullResolutionImage for high performance

    Example:

 CGImageRef iref = [myasset aspectRatioThumbnail];
 itemToAdd.image = [UIImage imageWithCGImage:iref];

Example:

-(void) findLargeImage:(NSNumber*) arrayIndex
{
 ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
 {

    CGImageRef iref = [myasset aspectRatioThumbnail];         

 dispatch_sync(dispatch_get_main_queue(), ^{

    itemToAdd = [[UIImageView alloc] initWithFrame:CGRectMake([arrayIndex intValue]*320, 0, 320, 320)];
    itemToAdd.image = [UIImage imageWithCGImage:iref];
    [self.scrollView addSubview:itemToAdd];

 });//end block


};
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"Cant get image - %@",[myerror localizedDescription]);
};
NSURL *asseturl = [NSURL URLWithString:[self.photoPath objectAtIndex:[arrayIndex intValue] ]];
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:asseturl 
               resultBlock:resultblock
              failureBlock:failureblock];

}

Also change the order of viewWillAppear()

- (void) viewWillAppear:(BOOL)animated {

    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [self findLargeImage:self.actualPhotoIndex];

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...