2011-10-08 102 views
0

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

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

任何一个可以告诉我一个很好的方法来处理它与捆绑的图像工作的问题

还有些时候控制台显示,由于节目的接收信号

应用程序崩溃:“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]; 

     } 
+0

很难看到你的代码格式很奇怪。该代码在哪里执行,这可能是此代码失败的一个重要方面。 – EricLeaf

+0

我在我的视图中添加了滚动视图。滚动视图包含9个UiView,每个UIView包含一个图像视图。当主要的UILoads(当点击RootView TableView单元格时),我需要在ScrollView中显示9个ImageViews。第一次,9个imageViews是空的。在动态重新加载后,我可以看到所有图像 – sham

+0

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

回答

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;