【问题标题】:ALAsset Photo Library Image Performance Improvements When Its Loading Slow加载缓慢时的 ALAsset 照片库图像性能改进
【发布时间】:2012-11-22 08:28:48
【问题描述】:

您好,我在滚动视图上显示图像时遇到问题。

首先,我使用资产 url 创建新的 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;

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

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

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

【问题讨论】:

  • 你检查它是否真的进入了结果块吗?
  • 是的,我确定我的方法会进入成功块,如果我将图像发送到另一个视图控制器,它将被显示。
  • __block 仅适用于局部变量。你正试图把它放在一个属性上。这没有意义。

标签: iphone ios objective-c-blocks


【解决方案1】:

ALAssetsLibrary 块将在单独的线程 中执行。所以我建议在主线程中做UI相关的东西。

为此,请使用 dispatch_sync(dispatch_get_main_queue()performSelectorOnMainThread

一些重要说明:

  1. 使用 AlAsset aspectRatioThumbnail 代替 fullResolutionImage 以获得高性能

    例子:

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

}

【讨论】:

    【解决方案2】:

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

    使用以下方法将图像添加到滚动视图:

    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

    【讨论】:

    • 我不知道为什么,但是当我使用第一种方法时,它会阻止我的应用程序,并且我无法按视图上的任何按钮。当我使用第二种方法时,它工作正常,但它将 itemToAdd subView 添加到我的 self.view 当我使用此方法将 itemToAdd 添加到 scrollView 然后将 scrollView 添加到 self.view 它不起作用。
    • 用self.scrollView替换self.view
    • @edzio27:我认为你使用的是 dispatch_sync 而不是 dispatch_async
    • @edzio27:还有问题吗?
    • 是的,我使用 [self.scrollView performSelectorOnMainThread:@selector(addSubview:) withObject:itemToAdd waitUntilDone:NO];在我的块和 [self.view addSubview:self.scrollView];在我看来WillAppear 仍然无法正常工作。
    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多