0

I am trying to fetch data from an URL (in this case an image) and write this data to the disk. I receive the data and everything seems fine. The Debugger even shows that data is written to the disk and it is even the right size. But when I open the photos app I cant find the file. Why?

var fileData: NSData = NSData(contentOfURL: response.URL!)!
var paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentsDirectory: NSString = paths.objectAtIndex(0) as NSString
var filePath = documentsDirectory.stringByAppendingPathComponent("new.jpg")
fileData.writeToFile(filePath, atomically: true)
zanzoken
  • 787
  • 4
  • 12
  • 18
  • 3
    Why would you expect the Photos app to be able to see _your_ file? It's in _your_ sandbox. The Photos app has its _own_ sandbox - its own library, its own world. – matt Feb 06 '15 at 15:43
  • Everything you save in your own app is separate from other apps on the device... https://developer.apple.com/library/mac/documentation/Security/Conceptual/AppSandboxDesignGuide/AboutAppSandbox/AboutAppSandbox.html – timgcarlson Feb 06 '15 at 15:44
  • How can then can I make it visible for other apps or even use the file outside of my app? – zanzoken Feb 06 '15 at 15:46
  • There is not a central location on the device for files like in a traditional file system, you will have to use other services for that sort of thing (i.e. iCloud). If all you're looking to do is save an image to the user's photo library, then there are tons of questions on SO explaining how to do this. Here is one: http://stackoverflow.com/questions/11131050/how-can-i-save-an-image-to-the-camera-roll – timgcarlson Feb 06 '15 at 15:52

1 Answers1

1

I think you can write to the photo app album with UIImageWriteToSavedPhotosAlbum. Of course you need to convert the data to UIImage first: [UIImage imageWithData: fileData]

Micky
  • 5,578
  • 7
  • 31
  • 55
  • I know it is a seperate question. But do you know how I would do that for video and audio files? – zanzoken Feb 06 '15 at 16:04
  • 1
    For video it's probably `UISaveVideoAtPathToSavedPhotosAlbum`. For audio, afaik you can't write to the iTunes library from a third party app. – Micky Feb 06 '15 at 16:07