2012-08-02 2 views
2

일부 이미지의 경우 ALAssetLibrary를 사용하여 사진을 가져올 때 AssetRepresentation.size가 0이되어 이미지를 만들지 않습니다. 코드는 다음과 같습니다.불러 오기 사진 alassetlibrary 자산 표현 크기가 0입니다.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 

    if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:self.groupName]) { 

     [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){ 
      //Get the asset type 
      NSString *assetType = [result valueForProperty:ALAssetPropertyType]; 
      if ([assetType isEqualToString:ALAssetTypePhoto]) { 
       NSLog(@"Photo Asset"); 
      } 
      //Get URLs for the assets 
      NSDictionary *assetURLs = [result valueForProperty:ALAssetPropertyURLs]; 

      NSUInteger assetCounter = 0; 
      for (NSString *assetURLKey in assetURLs) { 
       assetCounter++; 
      } 
      //Get the asset's representation object 
      ALAssetRepresentation *assetRepresentation = [result defaultRepresentation]; 

      //From this asset representation, we take out data for image and show it using imageview. 

      dispatch_async(dispatch_get_main_queue(), ^(void){ 
       CGImageRef imgRef = [assetRepresentation fullResolutionImage]; 
       //Img Construction 
       UIImage *image = [[[UIImage alloc] initWithCGImage:imgRef] autorelease]; 

       NSLog(@"before %@:::%lld", [image description], [assetRepresentation size]); //Prints '0' for size 

       if((image != nil)&& [assetRepresentation size] != 0){ 

        //display in image view 
       } 
       else{ 
        // NSLog(@"Failed to load the image."); 
       } 
      }); 

     }]; 
    } 
}failureBlock:^(NSError *error){ 
    NSLog(@"Error retrieving photos: %@", error); 
}]; 


[library release]; 

제발 도와주세요. 여기서 내가 뭘 잘못하고 있니? 이미지를 어떻게 얻을 수 있습니까?

답변

2

조 스미스가 옳습니다! 라이브러리를 너무 빨리 릴리스합니다. AssetLibrary가 해제되는 순간, 모든 자산 오브젝트가 사라질 것입니다. 그리고 이러한 열거 형은 블록 코드이므로 AssetLibrary의 릴리스는 읽기 프로세스 중에 어딘가에서 실행됩니다.

나는 당신이 ALAssetLibrary에서 ALAssetsLibraryChangedNotification을 살아 그것을 유지하고 변경 알림 를받을 경우에만 재설정 앱 위임에 assetLibrary를 만들 것을 제안

내가 열거 한 후 block..and가있어 경우를 출시
0

자산 라이브러리를 너무 빨리 릴리스했다고 생각합니다.

+0

너무 빨리 출시되면 크기가 0 인 자산을 표시하는 것보다 충돌이 발생해야합니다. 그래야하지 않니? –