延迟加载PhotoLibrary图像

时间:2011-10-08 15:03:59

标签: objective-c xcode image ipad photolibrary

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

因为我发现资产库块在另一个线程上工作。重新加载我的视图后,我可以看到所有图像。但是第一次图像视图为空。

任何人都可以告诉我处理问题的好方法

使用Bundle Images。

有时候控制台会显示

由于程序收到信号,

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

我的代码:

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

      }

1 个答案:

答案 0 :(得分:0)

我正在使用此方法在延迟加载的滚动视图中显示图像。效果很好。

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

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;