2016-10-10 4 views
2

내 Firebase 저장소에서 f5bd8360.jpeg 이미지를 다운로드하려고합니다.
dataWithMaxSize:completion을 사용하여이 이미지를 메모리에 다운로드하면 다운로드 할 수 있습니다.
Firebase 저장소 로컬 파일로 다운로드 오류

writeToFile: 인스턴스 메서드를 사용하여 이미지를 로컬 파일로 다운로드하려고하면 문제가 발생합니다.

나는 다음과 같은 오류 받고 있어요 : 여기

Optional(Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown 
error occurred, please check the server response." 
UserInfo={object=images/f5bd8360.jpeg, 
bucket=fir-test-3d9a6.appspot.com, NSLocalizedDescription=An unknown 
error occurred, please check the server response., 
ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/Documents/images, 
NSUnderlyingError=0x1700562c0 {Error Domain=NSPOSIXErrorDomain Code=1 
"Operation not permitted"}, ResponseErrorCode=513}" 

나의 스위프트 코드 조각입니다 : 내가지고있어 같은 오류 another question을 발견

@IBAction func buttonClicked(_ sender: UIButton) { 

     // Get a reference to the storage service, using the default Firebase App 
     let storage = FIRStorage.storage() 

     // Get reference to the image on Firebase Storage 
     let imageRef = storage.reference(forURL: "gs://fir-test-3d9a6.appspot.com/images/f5bd8360.jpeg") 

     // Create local filesystem URL 
     let localURL: URL! = URL(string: "file:///Documents/images/f5bd8360.jpeg") 

     // Download to the local filesystem 
     let downloadTask = imageRef.write(toFile: localURL) { (URL, error) -> Void in 
      if (error != nil) { 
       print("Uh-oh, an error occurred!") 
       print(error) 
      } else { 
       print("Local file URL is returned") 
      } 
     } 
    } 

있지만에 대답하지 않았다 완전한. 나는 그 제안이 옳다고 생각한다. 파일에 쓸 수있는 권한이 없습니다. 그러나, 나는 어떻게 허가를 얻는 지 모른다. 어떤 아이디어?

let downloadTask = imageRef.write(toFile: localURL) { (URL, error) -> Void in 
etc. 

아직 그 (localURL) 위치에 쓸 수있는 권한이 없습니다 :

+1

다른 답변에 따르면, 당신은'file : /// Documents/images/f5bd8360.jpeg'에 쓸 권한이 없을 가능성이 있습니다. '/ Documents'에 쓸 수있는 권한이 있지만'/ Documents/images'가 존재하지 않고 자동으로 생성되지 않기 때문에 파일을 쓸 수 없으므로 실패합니다. 쓸 수있는 모든 디렉토리가 있는지 확인하십시오. –

+2

[Firebase Storage에서 다운로드 할 수없는 이미지] (http://stackoverflow.com/questions/37539785/images-not-downloading-from-firebase-storage) –

+0

가능한 경우 iOS에서 디렉토리를 만들 수 있습니까? 특히 images 디렉토리. –

답변

6

문제는 순간에 당신이 줄을 쓸 때이다. 에게 당신이 실제에 테스트하는 경우 (장치에 다음과 같은 경로에 파일을 작성합니다 그 일을함으로써 localURL

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 
let localURL = documentsURL.appendingPathComponent("filename") 

에 아무것도 쓰지하기 전에 다음과 같은 코드를 작성하는 데 필요한 권한을 얻을 장치) : 파일 : ///var/mobile/Containers/Data/Application/XXXXXXXetc.etc./Documents/filename

시뮬레이터에서 테스트하는 경우 경로는 분명히 컴퓨터의 어딘가에 있습니다.

관련 문제