• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何从 UIImagePickerController 获取图像的 NSURL?上传到 Firebase

[复制链接]
菜鸟教程小白 发表于 2022-12-13 13:47:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

因此已经启动了一些其他线程,因为人们对此有疑问。喜欢这个: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"
    }



Best Answer-推荐答案


解决方案在这里: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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap