2014-07-26 33 views
3

我盡力了,但是我被卡在了這裏。我想從iPhone相機相冊導入所有照片。所以我想出了這個ALAssestsLibrary API。如何枚舉Swift中的ALAssetsGroup

photoLibrary.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: { group in 
     if group != nil 

     { 

     } 

     }, failureBlock: { error in println("\(error)")}) 

如何添加這行代碼。

group enumerateAssetsUsingBlock:groupEnumerAtion 

我試着添加這個,但它沒有顯示任何enumerateAssetsUsingBlock屬性。

這是實際的代碼。 !在Objective-C

dispatch_async(dispatch_get_main_queue(),^
     { 
      @autoreleasepool 
      { 
       ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
        { 
         NSLog(@"error occour =%@", [myerror localizedDescription]); 
       }; 

       ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
        { 
         if (result!=NULL) 
         { 
          if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) 
          { 
           [self.g_imageArray addObject:result]; 
          } 
         } 
       }; 

       ALAssetsLibraryGroupsEnumerationResultsBlock 
       libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop) 
        { 
         if (group == nil) 
         { 
          return; 
         } 

         if (group!=nil) { 
          [group enumerateAssetsUsingBlock:groupEnumerAtion]; 
         } 
         [self updatephotoList]; 
       }; 

       self.library = [[ALAssetsLibrary alloc] init]; 
       [self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos 
       usingBlock:libraryGroupsEnumeration 
       failureBlock:failureblock]; 
      } 
     }); 

回答

8

enumerationBlockfailureBlock沒有正確的類型。 例如,枚舉塊被定義爲

typealias ALAssetsLibraryGroupsEnumerationResultsBlock = (ALAssetsGroup!, UnsafeMutablePointer<ObjCBool>) -> Void 

這意味着該參數是一個閉包作爲參數,以(ALAssetsGroup!, UnsafeMutablePointer<ObjCBool>)並返回Void

{ 
    (group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
    // ... 
} 

所以,你的代碼應該是這樣的:

photoLibrary.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), 
    usingBlock: { 
     (group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
     if group != nil { 
      group.enumerateAssetsUsingBlock({ 
       (asset: ALAsset!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
        // ... 
       }) 
     } 
    }, 
    failureBlock: { 
     (myerror: NSError!) -> Void in 
     println("error occurred: \(myerror.localizedDescription)") 
    }) 

由於Swift的「自動類型推斷」功能,您可以這樣寫 as

photoLibrary.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), 
    usingBlock: { 
     group, stop in 
     if group != nil { 
      group.enumerateAssetsUsingBlock({ 
       asset, index, stop in 
       // ... 
       }) 
     } 
    }, 
    failureBlock: { 
     myerror in 
     println("error occurred: \(myerror.localizedDescription)") 
    }) 

但在這種情況下,第一個版本可能更容易理解。

+0

謝謝!不得不將UnsafePointer更改爲UnsafeMutablePointer雖然 – garafajon

+0

@garafajon:您完全正確,這在Swift版本之間發生了變化。我已經更新了答案。感謝您的反饋! –