AlAsset库的问题

时间:2011-11-01 14:57:21

标签: iphone alassetslibrary

我遇到了ALAsset库的问题:我有一个包含100个图像视图的UIView。加载视图时,我正在调用一个函数,用于从文件名生成图像。

这是我的班级:

@interface myClass
{
    NSString *fileName;
    int pathId;
}

viewDidLoad中

-(void)viewDidLoad
{
    NSMutableArray *imageCollectionArray = [self createImage:arrayOfmyClassObject];

     //Here I'm binding the 100 images in UIView using the images in imageCollectionArray
}

这是我发现问题的方法:

- (NSMutableArray *)createImage:(NSMutableArray *)imageFileNamesArray 
{
    imageArray = [[NSMutableArray alloc] init]; 
    for (int imageNameKey = 0; imageNameKey<100; imageNameKey++) 
    {
         myClass *obj= [imageFileNamesArray objectAtIndex:imageNameKey];
        if(obj.pathId == 0)
        {

            //Here adding the bundle image into the imageArray
                [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:obj.fileName ofType:@"png" inDirectory:@"Images"]]];
        }
        else
        {
                typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset); typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
             ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) {
             };
             ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
            CGImageRef iref = [rep fullResolutionImage];
            UIImage *images; 
            if (iref)
                {

            //Here adding the photo library image into the imageArray
             images = [UIImage imageWithCGImage:iref];
            [imageArray addObject:images];

            }
            else
            {
            //Here adding the Nofile.png image into the imageArray if didn't find a photo library image
              images = [UIImage imageNamed:@"Nofile.png"];
             [imageArray addObject:images];
            }

             ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) {

                        //Here adding the Nofile.png image into the imageArray if any failure occurs
                 [imageArray addObject:[UIImage imageNamed:@"Nofile.png"]]; 

            NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
             };
             NSURL *asseturl = [NSURL URLWithString:obj.fileName];
             ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; 
            [assetslibrary assetForURL:asseturl
             resultBlock:resultblock failureBlock:failureblock];
        }
    }
    return imageArray;
}

问题是当我第一次加载视图时没有生成资产库图像,只显示了包图像,如果我转到任何另一个视图并返回到100图像视图,则生成资产图像。工作正常。问题是相同的功能是在第一次加载时不生成资产图像。我怎样才能解决这个问题?提前谢谢。

1 个答案:

答案 0 :(得分:0)

与ALAssetLibrary相关的所有方法都是异步的,因此您的视图可能会在返回所需数据之前完成其加载生命周期。您必须考虑到这一点,并根据需要重绘您的视图(或其中的一部分)。