ALAsset Photo Library图像性能提升时加载缓慢 [英] ALAsset Photo Library Image Performance Improvements When Its Loading Slow

查看:137
本文介绍了ALAsset Photo Library图像性能提升时加载缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在我的scrollView上显示图像时出现问题。

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

首先我用资产网址创建新的UIImageView:

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];

}

其中itemToAdd是UIImageView在界面中定义:

Where itemToAdd is a UIImageView define in interface:

__block UIImageView *itemToAdd;

并且scrollView定义为属性:

And scrollView define as a property:

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

然后在我的viewWillAppear中我这样做:

Then in my viewWillAppear i do this:

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

但是图像没有出现,我应该在将图像添加到scrollView后刷新self.view或者应该做些什么?

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

推荐答案

ALAssetsLibrary 块将在单独的线程中执行即可。因此,我建议在主线程中执行与UI相关的内容。

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

要执行此操作,请使用 dispatch_sync(dispatch_get_main_queue() performSelectorOnMainThread

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

一些重要说明:


  1. 使用AlAsset aspectRatioThumbnail而不是fullResolutionImage获得高性能

  1. Use AlAsset aspectRatioThumbnail instead of fullResolutionImage for high performance

示例:




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


示例:

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

}

同时更改viewWillAppear()的顺序

Also change the order of viewWillAppear()

- (void) viewWillAppear:(BOOL)animated {

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

}

这篇关于ALAsset Photo Library图像性能提升时加载缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆