2011-11-01 81 views
0

我面臨的問題與ALAsset庫:我有一個UIView 100圖像視圖。當視圖加載時,我正在調用一個用於從文件名生成圖像的函數。問題與AlAsset庫

這是我的課:

@interface myClass 
{ 
    NSString *fileName; 
    int pathId; 
} 

viewDidLoad中

-(void)viewDidLoad 
{ 
    NSMutableArray *imageCollectionArray = [self createImage:arrayOfmyClassObject]; 

    //Here I'm binding the 100 images in UIView using the images in imageCollectionArray 
} 

這是我的方法,我發現了問題:

- (NSMutableArray *)createImage:(NSMutableArray *)imageFileNamesArray 
{ 
    imageArray = [[NSMutableArray alloc] init]; 
    for (int imageNameKey = 0; imageNameKey<100; imageNameKey++) 
    { 
     myClass *obj= [imageFileNamesArray objectAtIndex:imageNameKey]; 
     if(obj.pathId == 0) 
     { 

      //Here adding the bundle image into the imageArray 
       [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:obj.fileName ofType:@"png" inDirectory:@"Images"]]]; 
     } 
     else 
     { 
       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) 
       { 

      //Here adding the photo library image into the imageArray 
      images = [UIImage imageWithCGImage:iref]; 
      [imageArray addObject:images]; 

      } 
      else 
      { 
      //Here adding the Nofile.png image into the imageArray if didn't find a photo library image 
       images = [UIImage imageNamed:@"Nofile.png"]; 
      [imageArray addObject:images]; 
      } 

      ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) { 

         //Here adding the Nofile.png image into the imageArray if any failure occurs 
       [imageArray addObject:[UIImage imageNamed:@"Nofile.png"]]; 

      NSLog(@"booya, cant get image - %@",[myerror localizedDescription]); 
      }; 
      NSURL *asseturl = [NSURL URLWithString:obj.fileName]; 
      ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; 
      [assetslibrary assetForURL:asseturl 
      resultBlock:resultblock failureBlock:failureblock]; 
     } 
    } 
    return imageArray; 
} 

的問題是,當我負載第一次查看資產庫圖片不會生成,只顯示包圖像,如果我轉到任何其他視圖並返回到100圖像視圖,則生成資產圖像。並且工作正常。問題是相同的功能在第一次加載時不會生成資產映像。我怎樣才能解決這個問題?提前致謝。

+0

我不相信這段代碼可以編譯。 – jamapag

+1

無法理解您的代碼。隨處可見括號。 – memmons

+0

奇怪的代碼...不要忘了ALAsset是異步的,也許你需要在加載所有圖像時執行setNeedsDisplay – Johnmph

回答

0

與ALAssetLibrary相關的所有方法都是異步的,因此您的視圖可能會在返回所需數據之前完成其加載生命週期。您必須考慮到這一點,並根據需要重新繪製您的視圖(或其中的一部分)。