【问题标题】:Lazy loading of PhotoLibrary ImagesPhotoLibrary 图像的延迟加载
【发布时间】:2011-10-08 15:03:58
【问题描述】:

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

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

谁能告诉我解决问题的好方法

它适用于捆绑图像。

有时控制台也会显示

由于程序接收到的信号:“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];

      }

【问题讨论】:

  • 很难阅读格式如此奇怪的代码。这段代码在哪里执行,这可能是这段代码失败的一个重要方面。
  • 我在我的视图中添加了一个滚动视图。滚动视图包含 9 个 UiView,每个 UIView 包含一个图像视图。当主 UILoads(单击 RootView TableView 单元格时)我需要在 ScrollView 中显示 9 个 ImageView。第一次,9 个图像视图是空的。再次动态重新加载后,我可以看到所有图像
  • 有时应用程序由于程序接收到的信号而崩溃:“0”。数据格式化程序暂时不可用,将在“继续”后重试。 (加载共享库“/Developer/usr/lib/libXcodeDebuggerSupport.dylib”时出现未知错误)

标签: objective-c xcode image ipad photolibrary


【解决方案1】:

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

首先初始化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;

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 2014-10-31
    • 2020-08-12
    相关资源
    最近更新 更多