2014-12-27 66 views
0

我想加載並顯示照片庫(相機膠捲)的最後一張照片在UIImageView,一切工作正常!除了一件事!如果圖庫中沒有可用的圖像,則應用程序崩潰!這裏是我的代碼:檢測照片庫是空的

-(void)importLastImage { 

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos. 
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 

     // Within the group enumeration block, filter to enumerate just photos. 
     [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 

     // Chooses the photo at the last index 
     [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)] 
           options:0 
          usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) { 

           // The end of the enumeration is signaled by asset == nil. 
           if (alAsset) { 
            ALAssetRepresentation *representation = [alAsset defaultRepresentation]; 
            latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]]; 

            _lastImage.image = latestPhoto; 

           }else { 

          //no image found ! 

           } 
     }]; 
    } 
         failureBlock: ^(NSError *error) { 
          // Typically you should handle an error more gracefully than this. 
          NSLog(@"No groups"); 

          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"No Image found" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; 
          [alert show]; 

}]; 

} 
+0

嘗試加入一些條件沿着'如果([組numberOfAssets])'調用'enumerateAssetsAtIndexes前行:選項:usingBlock:',看看它是否解決了您問題。 – AMI289

回答

1

我決定在這裏發佈一個代碼片段,您可以複製粘貼,其中包含我上面評論的建議。
對不起,如果我的英語不夠清晰(我不是英語母語人士)。

你的崩潰是由於該行代碼
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]

的問題是,正如你所說的,當有庫沒有可用的圖像,numberOfAssets返回0,
既然你是與numberOfAssets - 1創建索引,你基本上是試圖創建一個負面的索引,它會崩潰你的程序。

我只是增加了一個if聲明,以檢查「如果」 numberOfAssets(這意味着它不爲0),
只有這樣,執行下面列舉,因此,防止負折射率任何情況下。

不管怎麼說,在這裏你去隊友:

-(void)importLastImage { 

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos. 
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 

     // Within the group enumeration block, filter to enumerate just photos. 
     [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 

     if([group numberOfAssets]) { 
      // Chooses the photo at the last index 
      [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)] 
            options:0 
           usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) { 

            // The end of the enumeration is signaled by asset == nil. 
            if (alAsset) { 
             ALAssetRepresentation *representation = [alAsset defaultRepresentation]; 
             latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]]; 

             _lastImage.image = latestPhoto; 

            }else { 

           //no image found ! 

            } 
      }]; 
     } else { 
      NSLog(@"No images in photo library"); 
     } 
    } 
         failureBlock: ^(NSError *error) { 
          // Typically you should handle an error more gracefully than this. 
          NSLog(@"No groups"); 

          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"No Image found" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; 
          [alert show]; 

    }]; 

}