当尝试加载和显示刚刚拍摄的照片的缩略图时:
func photom(){
if let videoConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { (imageDataSampleBuffer, error) -> Void in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
let lPhoto = UIImage(data: imageData)
var imageIdentifier: String?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(lPhoto!)
let placeHolder = changeRequest.placeholderForCreatedAsset
imageIdentifier = placeHolder!.localIdentifier
}, completionHandler: { success, error in
if success {
self.currentSelectedID = imageIdentifier!
self.makeSelectedItemButton(0)
return
}
})
}
}
}
其中makeSelectedItemButton()是:
func makeSelectedItemButton(layConst: CGFloat){
if let selButto = view.viewWithTag(6){
selButto.removeFromSuperview()
}
PhotoHelper().getThumbnailWithIdentifer(currentSelectedID, groesse: CGSize(width: cellHocR ,height: cellHocR), completion: { (image) -> Void in
if image != nil {
let libView = UIImageView()
libView.frame = CGRectMake(self.abstanR, self.abstanR, 22, 22)
libView.layer.cornerRadius = 11
libView.clipsToBounds = true
libView.contentMode = .ScaleAspectFill
libView.image = image
let libButton = UIButton()
libButton.backgroundColor = UIColor.clearColor()
libButton.tag = 6
libButton.frame = CGRectMake(0+layConst, self.view.frame.height-self.cellHocR, self.cellHocR, self.cellHocR)
libButton.addTarget(self, action: "libTap:", forControlEvents: .TouchUpInside)
libButton.insertSubview(libView, atIndex: 0)
self.view.addSubview(libButton)
}
})
}
PhotoHelper ().getThumbnailWithIdentifer是:
func getThumbnailWithIdentifer(localIdentifier:String, groesse: CGSize, completion: (image:UIImage?) -> Void) {
let manager = PHImageManager.defaultManager()
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType == %d", PHAssetMediaType.Image.rawValue)
let fetchResults = PHAsset.fetchAssetsWithLocalIdentifiers([localIdentifier], options: fetchOptions)
if fetchResults.count > 0 {
if let imageAsset = fetchResults.objectAtIndex(0) as? PHAsset {
let requestOptions = PHImageRequestOptions()
requestOptions.synchronous = true
requestOptions.deliveryMode = .HighQualityFormat
manager.requestImageForAsset(imageAsset, targetSize: groesse, contentMode: .AspectFit, options: requestOptions, resultHandler: { (image, info) -> Void in
completion(image: image)
print(image!.size)
})
} else {
completion(image: nil)
}
} else {
completion(image: nil)
}
}
这会导致按钮在照片制作后至少显示 5 秒。使用旧照片执行时,相同的功能可以快速且按预期工作。这里有什么问题?