Uiview 在Swift 2中拍摄照片后显示缩略图
当尝试加载和显示刚刚拍摄的照片的缩略图时,如下所示:Uiview 在Swift 2中拍摄照片后显示缩略图,uiview,swift2,phasset,phphotolibrary,Uiview,Swift2,Phasset,Phphotolibrary,当尝试加载和显示刚刚拍摄的照片的缩略图时,如下所示: func photom(){ if let videoConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) { self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { (imageDataSample
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)
}
})
}
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)
}
}
和PhotoHelper()。getThumbnailWithIdentifer是:
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)
}
})
}
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秒显示。当使用旧照片时,相同的功能运行得很快,并且与预期的一样。这里有什么问题?由于getThumbnailWithIdentifer中的以下行,您是否正在等待:
requestOptions.synchronous = true
您可以将其更改为
false
,并尝试使用占位符缩略图图像,直到实际缩略图返回为止?对不起,我很久以前就通过调度解决了这个问题。我不知道你的解决方案是否有效,但谢谢你!