ALAsset照片库图像性能改进加载缓慢

时间:2012-11-22 08:28:48

标签: iphone ios objective-c-blocks

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

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

-(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定义:

__block UIImageView *itemToAdd;

并且scrollView定义为属性:

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

然后在我看来我会这样做:

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

但是图像没有出现,我应该在将图像添加到scrollView之后刷新self.view,还是应该做其他事情?

2 个答案:

答案 0 :(得分:4)

ALAssetsLibrary 块将在单独的帖子中执行。所以我建议在主线程中执行与UI相关的内容。

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

一些重要说明:

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

    示例:

  2.  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()

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

答案 1 :(得分:0)

您正在操纵另一个线程的视图。 您必须使用主线程来操纵视图。

使用以下方法将图像添加到scrollView:

dispatch_async(dispatch_get_main_queue(), ^{
   [self.scrollView addSubview:itemToAdd];
}

或使用:

[self.scrollView performSelectorOnMainThread:@selector(addSubview:) withObject:itemToAdd waitUntilDone:NO];

请参阅:

  1. NSObject Class Reference
  2. GCD