【问题标题】:issue with AlAsset libraryAlAsset 库的问题
【发布时间】:2011-11-01 14:57:20
【问题描述】:

我遇到了 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 个图像视图,则生成资产图像.并且工作正常。问题是相同的功能在第一次加载时不会生成资产图像。我怎样才能解决这个问题?提前致谢。

【问题讨论】:

  • 我不相信这段代码可以编译。
  • 无法理解您的代码。括号无处不在。
  • 奇怪的代码...不要忘记 ALAsset 是异步的,也许你需要在所有图像加载时做一个 setNeedsDisplay
  • @adrian:我也在使用 ALAsset 库。您如何解决将 ALAsset 对象中的一堆图像加载到数组中的内存问题?你能在你的个人资料中联系我吗?我看不到你的电子邮件?谢谢!

标签: iphone alassetslibrary


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多