问题
I'm new to Swift. I'm trying to use the method signature in my class:
func UISaveVideoAtPathToSavedPhotosAlbum(_ videoPath: String!,
_ completionTarget: AnyObject!,
_ completionSelector: Selector,
_ contextInfo: CMutableVoidPointer)
The last argument, contextInfo, is a void * in obj-c.
I get an NSDictionary
not a subtype of CMutableVoidPointer
error if I pass a dictionary in Swift. Would appreciate any help with this. I don't know how to pass a void
* equivalent argument in Swift without getting that error.
回答1:
Objective C - Void*
Swift - UnsafeMutablePointer
.. Example
// Your call to save video to camera roll
UISaveVideoAtPathToSavedPhotosAlbum(yourVideoPath, self, "video:didFinishSavingWithError:contextInfo:", nil)
// Your Completion Handler after video is saved
func video(videoPath: String, didFinishSavingWithError error: NSError, contextInfo info: UnsafeMutablePointer<Void>) {
// your completion code handled here
}
回答2:
// Use `var`, not `let`
var userInfo: NSDictionary = ....
UISaveVideoAtPathToSavedPhotosAlbum(..., ..., ..., &userInfo)
CMutableVoidPointer
is void *
CConstVoidPointer
is const void *
回答3:
Swift 4.2
You can pass a NSDictionary as an UnsafeMutablePointer following a few steps:
1) declare a dictionary as a property of your view controller
var userInfo: NSDictionary = [:]
2) Second you will need to declare a method to be called when your picker is finished saving your image/video asynchronously:
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
print(#function)
let infoDict = Unmanaged<NSDictionary>
.fromOpaque(contextInfo)
.takeUnretainedValue()
as? [String: Any] ?? [:]
print("infoDict", infoDict )
}
3) You will need to convert your NSDictionary to a UnsafeMutableRawPointer:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
print(#function)
if let img = info[.originalImage] as? UIImage {
userInfo = info as NSDictionary
UIImageWriteToSavedPhotosAlbum(img,
self,
#selector(image),
UnsafeMutableRawPointer(
Unmanaged
.passUnretained(userInfo)
.toOpaque()))
}
dismiss(animated: true)
}
来源:https://stackoverflow.com/questions/24471744/ios-passing-a-void-into-uisavevideoatpathtosavedphotosalbum-in-swift