我只想使用 Swift 从 ios 8 中的 ALAsset 获取图像资源。
我使用 SNImagePicker 进行多张图片选取。
对于 objective-c,我使用这个:
for (int i = 0; i < info.count; i++) {
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:info[i] resultBlock:^(ALAsset *asset) {
UIImage *image = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
} failureBlock:^(NSError *error) { }];
使用 swift,我尝试这样做:
func imagePicker(imagePicker: SNImagePickerNC!, didFinishPickingWithMediaInfo info: NSMutableArray!){
var i: Int = Int(0);
for (i; i < info.count as Int; i++){
var url: NSURL = info[i] as NSURL
var assetLibrary: ALAssetsLibrary = ALAssetsLibrary()
//
//assetLibrary.assetForURL(NSURL?(), resultBlock: ALAssetsLibraryAssetForURLResultBlock?(), failureBlock: ALAssetsLibraryAccessFailureBlock?())
//
assetLibrary.assetForURL(url, resultBlock: {
(asset: ALAsset!) in
if asset != nil {
var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
var iref = assetRep.fullResolutionImage().takeUnretainedValue()
var image = UIImage(CGImage: iref)
self.imageViewPreview.contentMode = UIViewContentMode.ScaleAspectFill
self.imageViewPreview.image = image
}
}, failureBlock: {
(error: NSError!) in
NSLog("Error!", nil)
}
)
}
}
通过这段代码,我得到了一个错误:
Type 'CVarArg' does not conform to protocol 'NilLiteralConvertible'
最佳答案
尝试用 NSLog("Error!") 替换 NSLog("Error!", nil)。此语句导致 Swift 抛出意外错误消息。
关于IOS Swift assetForURL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24821101/