首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhotoLibrary图像的延迟加载

PhotoLibrary图像的延迟加载
EN

Stack Overflow用户
提问于 2011-10-08 15:03:58
回答 1查看 843关注 0票数 0

我发现了照片库图片的问题。它没有第一次在我的视图中显示,图像视图在第一次加载时是空白的。

因为我发现Asset块正在处理另一个thread.After重新加载我的视图,所以我可以看到所有的图像。然而,第一次的图像视图是空白。

有人能告诉我处理这个问题的好方法吗?

它与捆绑图片一起工作。

也有一些时候控制台显示

由于程序接收信号:“0”,应用程序正在崩溃。数据格式暂时不可用,将在“继续”后重试。(加载共享库"/Developer/usr/lib/libXcodeDebuggerSupport.dylib")时未知错误)

我的守则:

代码语言:javascript
复制
  for (int j = 0; j<9; j++)
                {

          //allocating View         
           UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(xCordImage, yCordImage, 200, 190)];




            // allocating ImageView
            imageViewTopic = [[[UIImageView alloc] init] autorelease];                      


            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) {

                        images = [UIImage imageWithCGImage:iref];                               

                    }

                    else {

                    images = [UIImage imageNamed:@"Nofile.png"];

                    }



                imageViewTopic .image  = images ;



            };



            ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
                                    {

                    imageViewTopic .image  = [UIImage imageNamed:@"Nofile.png"];



                NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
                };    



                    NSString *string ;

                     MyClass *obj = [imageFileNameArray objectAtIndex:j];

                    **//obj.fileName contains ALAsset URL of a Image**
                    string = obj.fileName;

                    NSURL *asseturl = [NSURL URLWithString:string];
                    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
                  [assetslibrary assetForURL:asseturl                                      resultBlock:resultblock

                                                  failureBlock:failureblock];











       imageViewTopic.userInteractionEnabled = YES;


       imageViewTopic.frame = CGRectMake(0,0, 200, 150);


        [currentView addSubview:scroller];


         **// adding the imageView to View**
        [smallView addSubview:imageViewTopic];

        [myView addSubview:smallView];
             [scroller addSubview:myView];

      }
EN

回答 1

Stack Overflow用户

发布于 2012-09-05 09:13:03

我使用这种方法在滚动视图中以延迟加载方式显示图像。效果很好。

首先初始化j1的值。数据是来自数组循环的图像数据。

代码语言:javascript
复制
dispatch_async(dispatch_get_global_queue(0,0), ^{

             NSData * data = [[NSData alloc] initWithContentsOfURL:url];
             if ( data == nil )
                 return;
             dispatch_async(dispatch_get_main_queue(), ^{

                 __block int j1=_j;
                 // WARNING: is the cell still using the same data by this point??

                 //  NSURL *url = [NSURL URLWithString: imageName];
                 UIImage *image = [UIImage imageWithData: data]; //image.size.height
               image1=[[UIImageView alloc] initWithFrame:CGRectMake(j1,10,image.size.width,image.size.height)];

                 image1.image=image;

                 CALayer *layer = [image1 layer];
                 [layer setMasksToBounds:YES];
                 [layer setCornerRadius:0.0]; //note that when radius is 0, the border is a rectangle
                 [layer setBorderWidth:3.0];
                 [layer setBorderColor:[[UIColor whiteColor] CGColor]];


                 [portfolio_scroll addSubview:image1];

             }); 

         });
         _j = _j+ 320;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7697666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档