因此已经启动了一些其他线程,因为人们对此有疑问。喜欢这个:Obtain NSURL from UIImagePickerController和这个: Firebase storage upload works in Simulator but not on iPhone
我正在尝试遵循本教程: https://firebase.google.com/docs/storage/ios/upload-files#upload_from_a_local_file
我的视频可以上传,只是图片不行。
我的视频代码很简单:
if mediaType.isEqualToString(kUTTypeMovie as String) {
let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL
fileToUploadURL = videoURL!
}
但我的图像代码不起作用。这就是我最终得到的结果:
let imageUrl = info["UIImagePickerControllerReferenceURL"] as? NSURL
let assets = PHAsset.fetchAssetsWithALAssetURLs([imageUrl!], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
let imageFile = contentEditingInput?.fullSizeImageURL
// now call putFile with imageFile instead of imageUrl
self.fileToUploadURL = imageFile
})
似乎无论我做什么都会出错。 这是我当前的错误
2016-06-25 12:45:23.695 Chain[4187:1957121] Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG
Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0027.JPG” couldn’t be opened because you don’t have permission to view it."
控制台日志
2016-06-25 12:47:59.108 Chain[4187:1957121] Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0027.JPG” couldn’t be opened because you don’t have permission to view it." UserInfo={NSURL=file:///var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG, NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG, NSUnderlyingError=0x18b7ad40 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} 2016-06-25 12:48:02.969 Chain[4187:1957659] plugin com.swiftkey.SwiftKeyApp.Keyboard invalidated Optional(["public.image", "public.movie"]) info: ["UIImagePickerControllerOriginalImage": size {1836, 1454} orientation 0 scale 1.000000, "UIImagePickerControllerReferenceURL": assets-library://asset/asset.JPG?id=5FE3480C-0A3E-42B2-B83B-93D0F321A3B2&ext=JPG, "UIImagePickerControllerMediaType": public.image, "UIImagePickerControllerCropRect": NSRect: {{0, 0}, {1836, 1452}}, "UIImagePickerControllerEditedImage": size {638, 504} orientation 0 scale 1.000000] fileToUploadURL: file:///var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG Optional(["public.image", "public.movie"]) 2016-06-25 13:01:58.845 Chain[4187:1958957] plugin com.swiftkey.SwiftKeyApp.Keyboard invalidated info: ["UIImagePickerControllerOriginalImage": size {1836, 1454} orientation 0 scale 1.000000, "UIImagePickerControllerReferenceURL": assets-library://asset/asset.JPG?id=CFF5692E-2B93-407D-A9E4-48AC1BEDCAF2&ext=JPG, "UIImagePickerControllerMediaType": public.image, "UIImagePickerControllerCropRect": NSRect: {{0, 0}, {1836, 1452}}, "UIImagePickerControllerEditedImage": size {638, 504} orientation 0 scale 1.000000] fileToUploadURL: nil
2016-06-25 13:04:05.136 Chain[4187:1957121] _BSMachError: (os/kern) invalid capability (20) 2016-06-25 13:04:05.138 Chain[4187:1959394] _BSMachError: (os/kern) invalid name (15) 2016-06-25 13:04:05.146 Chain[4187:1959394] plugin com.swiftkey.SwiftKeyApp.Keyboard invalidated 2016-06-25 13:04:05.220 Chain[4187:1957188] CFNetwork SSLHandshake failed (-9806) 2016-06-25 13:04:05.661 Chain[4187:1957121] Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/IMG_0015.JPG Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0015.JPG” couldn’t be opened because you don’t have permission to view it." UserInfo={NSURL=file:///var/mobile/Media/DCIM/100APPLE/IMG_0015.JPG, NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0015.JPG, NSUnderlyingError=0x18bd4330 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
编辑:这是我的 didFinishPickingMedia:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
print ("info: \(info)")
if mediaType.isEqualToString(kUTTypeImage as String) {
var newImage: UIImage
if let possibleImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
newImage = possibleImage
}
else if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
newImage = possibleImage
}
else {
return
}
/// CAN NOT GET NSURL working, NEED TO USE NSDATA
if let jpegData:NSData = UIImageJPEGRepresentation(newImage, 80) {
currentMediaTypeToUpload = "img"
fileToUploadDATA = jpegData
}
}
else if mediaType.isEqualToString(kUTTypeMovie as String) {
let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL
fileToUploadURL = videoURL!
currentMediaTypeToUpload = "vid"
}
解决方案在这里:how do I get NSURL for an image froom UIImagePickerController? for upload to Firebase
应用于我们的问题:
if let referenceUrl = info[UIImagePickerControllerReferenceURL] {
////
let assetURL = NSURL(string: String(referenceUrl))
let assets = PHAsset.fetchAssetsWithALAssetURLs([assetURL!], options: nil)
let asset: PHAsset = assets.firstObject as! PHAsset
let manager = PHImageManager.defaultManager()
let requestSize = PHImageManagerMaximumSize
manager.requestImageForAsset(asset, targetSize: requestSize, contentMode: .AspectFit, options: nil, resultHandler: {(result, info)->Void in
// Result is a UIImage
// Either upload the UIImage directly or write it to a file
let imageData = UIImageJPEGRepresentation(result!, 0.8)
let imagePath = FIRAuth.auth()!.currentUser!.uid + "/\(Int(NSDate.timeIntervalSinceReferenceDate() * 1000)).jpg"
print ("filePath: ", imagePath)
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
self.storageRef.child(imagePath)
.putData(imageData!, metadata: metadata) { (metadata, error) in
if let error = error {
print("Error uploading: \(error.description)")
return
}
self.sendMessage([Constants.MessageFields.imageUrl: self.storageRef.child((metadata?.path)!).description])
print("Succeded")
}
})
}
有了这个解决方案,我可以将我的文件上传到 firebase。
关于ios - 如何从 UIImagePickerController 获取图像的 NSURL?上传到 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030831/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |