2011-10-08 105 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;