菜鸟教程小白 发表于 2022-12-13 13:47:20

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


                                            <p><p>因此已经启动了一些其他线程,因为人们对此有疑问。喜欢这个:<a href="https://stackoverflow.com/questions/37670009/obtain-nsurl-from-uiimagepickercontroller" rel="noreferrer noopener nofollow">Obtain NSURL from UIImagePickerController</a>和这个:
<a href="https://stackoverflow.com/questions/37603312/firebase-storage-upload-works-in-simulator-but-not-on-iphone?rq=1" rel="noreferrer noopener nofollow">Firebase storage upload works in Simulator but not on iPhone</a> </p>

<p>我正在尝试遵循本教程:
<a href="https://firebase.google.com/docs/storage/ios/upload-files#upload_from_a_local_file" rel="noreferrer noopener nofollow">https://firebase.google.com/docs/storage/ios/upload-files#upload_from_a_local_file</a> </p>

<p>我的视频可以上传,只是图片不行。</p>

<p>我的视频代码很简单:</p>

<pre><code>if mediaType.isEqualToString(kUTTypeMovie as String) {
      let videoURL = info as? NSURL

      fileToUploadURL = videoURL!
    }
</code></pre>

<p>但我的图像代码不起作用。这就是我最终得到的结果:</p>

<pre><code>      let imageUrl = info[&#34;UIImagePickerControllerReferenceURL&#34;] as? NSURL

      let assets = PHAsset.fetchAssetsWithALAssetURLs(, 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
      })
</code></pre>

<p>似乎无论我做什么都会出错。
这是我当前的错误</p>

<pre><code>2016-06-25 12:45:23.695 Chain Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG
Error Domain=NSCocoaErrorDomain Code=257 &#34;The file “IMG_0027.JPG”       couldn’t be opened because you don’t have permission to view it.&#34;
</code></pre>

<p>控制台日志</p>

<blockquote>
<p>2016-06-25 12:47:59.108 Chain Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG
    Error Domain=NSCocoaErrorDomain Code=257 &#34;The file “IMG_0027.JPG” couldn’t be opened because you don’t have permission to view it.&#34; 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 &#34;Operation not permitted&#34;}}
2016-06-25 12:48:02.969 Chain plugin com.swiftkey.SwiftKeyApp.Keyboard invalidated
Optional([&#34;public.image&#34;, &#34;public.movie&#34;])
info: [&#34;UIImagePickerControllerOriginalImage&#34;:size {1836, 1454} orientation 0 scale 1.000000, &#34;UIImagePickerControllerReferenceURL&#34;: assets-library://asset/asset.JPG?id=5FE3480C-0A3E-42B2-B83B-93D0F321A3B2&amp;ext=JPG, &#34;UIImagePickerControllerMediaType&#34;: public.image, &#34;UIImagePickerControllerCropRect&#34;: NSRect: {{0, 0}, {1836, 1452}}, &#34;UIImagePickerControllerEditedImage&#34;:size {638, 504} orientation 0 scale 1.000000]
fileToUploadURL: file:///var/mobile/Media/DCIM/100APPLE/IMG_0027.JPG
Optional([&#34;public.image&#34;, &#34;public.movie&#34;])
2016-06-25 13:01:58.845 Chain plugin com.swiftkey.SwiftKeyApp.Keyboard invalidated
info: [&#34;UIImagePickerControllerOriginalImage&#34;:size {1836, 1454} orientation 0 scale 1.000000, &#34;UIImagePickerControllerReferenceURL&#34;: assets-library://asset/asset.JPG?id=CFF5692E-2B93-407D-A9E4-48AC1BEDCAF2&amp;ext=JPG, &#34;UIImagePickerControllerMediaType&#34;: public.image, &#34;UIImagePickerControllerCropRect&#34;: NSRect: {{0, 0}, {1836, 1452}}, &#34;UIImagePickerControllerEditedImage&#34;:size {638, 504} orientation 0 scale 1.000000]
fileToUploadURL: nil</p>

<p>2016-06-25 13:04:05.136 Chain _BSMachError: (os/kern) invalid capability (20)
2016-06-25 13:04:05.138 Chain _BSMachError: (os/kern) invalid name (15)
2016-06-25 13:04:05.146 Chain plugin com.swiftkey.SwiftKeyApp.Keyboard invalidated
2016-06-25 13:04:05.220 Chain CFNetwork SSLHandshake failed (-9806)
2016-06-25 13:04:05.661 Chain Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/IMG_0015.JPG
    Error Domain=NSCocoaErrorDomain Code=257 &#34;The file “IMG_0015.JPG” couldn’t be opened because you don’t have permission to view it.&#34; 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 &#34;Operation not permitted&#34;}}</p>
</blockquote>

<p>编辑:这是我的 didFinishPickingMedia:</p>

<pre><code>func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: ) {


    let mediaType = info as! NSString

    print (&#34;info: \(info)&#34;)

    if mediaType.isEqualToString(kUTTypeImage as String) {
      var newImage: UIImage

      if let possibleImage = info[&#34;UIImagePickerControllerEditedImage&#34;] as? UIImage {
            newImage = possibleImage
      }
      else if let possibleImage = info[&#34;UIImagePickerControllerOriginalImage&#34;] as? UIImage {
            newImage = possibleImage
      }

      else {
            return
      }

      /// CAN NOT GET NSURL working, NEED TO USE NSDATA
      if let jpegData:NSData = UIImageJPEGRepresentation(newImage, 80) {
            currentMediaTypeToUpload = &#34;img&#34;
            fileToUploadDATA = jpegData
      }

    }


    else if mediaType.isEqualToString(kUTTypeMovie as String) {
      let videoURL = info as? NSURL

      fileToUploadURL = videoURL!
      currentMediaTypeToUpload = &#34;vid&#34;
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>解决方案在这里:<a href="https://stackoverflow.com/questions/38030831/how-do-i-get-nsurl-for-an-image-froom-uiimagepickercontroller-for-upload-to-fir" rel="noreferrer noopener nofollow">how do I get NSURL for an image froom UIImagePickerController? for upload to Firebase</a> </p>

<p>应用于我们的问题:</p>

<pre><code>iflet referenceUrl = info {

      ////
      let assetURL = NSURL(string: String(referenceUrl))
      let assets = PHAsset.fetchAssetsWithALAssetURLs(, 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)-&gt;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 + &#34;/\(Int(NSDate.timeIntervalSinceReferenceDate() * 1000)).jpg&#34;
            print (&#34;filePath: &#34;, imagePath)
            let metadata = FIRStorageMetadata()
            metadata.contentType = &#34;image/jpeg&#34;
            self.storageRef.child(imagePath)
                .putData(imageData!, metadata: metadata) { (metadata, error) in
                  if let error = error {
                        print(&#34;Error uploading: \(error.description)&#34;)
                        return
                  }
                  self.sendMessage()
                  print(&#34;Succeded&#34;)
            }

      })
    }
</code></pre>

<p>有了这个解决方案,我可以将我的文件上传到 firebase。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何从 UIImagePickerController 获取图像的 NSURL?上传到 Firebase,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38030831/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38030831/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何从 UIImagePickerController 获取图像的 NSURL?上传到 Firebase