AlAsset 库的问题

发布于 2024-12-13 03:42:38 字数 2741 浏览 1 评论 0原文

我面临 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 个图像视图,然后生成资产图像。并且工作正常。问题是相同的函数在第一次加载时不会生成资产图像。我该如何解决这个问题?提前致谢。

I'm facing an issue with the ALAsset library: I have an UIView with 100 image views. When the view is loading, i'm calling a function for generating the images from the file name.

This is my class:

@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
}

This is my method in which I found the issue:

- (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;
}

The problem was when I loads the view at first time the asset library images are not generating, only bundle images were displayed, if i go to any of the another view and return back to 100 image view then the asset images are generated.And works fine. The problem is the same function is not generating asset images at the first load. How can i fix this? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅哥哥的热头脑 2024-12-20 03:42:38

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

All methods related to ALAssetLibrary are asynchronous, so your view may complete its loading life cycle before the desired data is returned. You have to take this into account and redraw your view (or a portion of it) as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文