0

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

首先,我使用资产 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,还是应该做其他事情?

4

2 回答 2

4

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

}
于 2012-11-22T08:51:40.673 回答
0

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

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

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

或使用:

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

请参考:

  1. NSObject 类参考
  2. GCD
于 2012-11-22T08:51:03.433 回答