views:

486

answers:

1

hello guys,

i want to save a photo file from a given url to the documents folder of my app without giving it a filename just the url and it saves to disk, is that possible ?

or do i have to get the data from the url and then parse it as UIImage then save that image to disk and give it a filename ?

if there is no direct way, how can you extract the filename form the url ( is there any built in method that can do that ) ?

thanks, and appreciate your help!

+1  A: 

You can just save the raw data from the url without formatting as any particular image.

The file has to have a name because the name is part of the path to the file. If you don't care about the actual name of the file, the easiest way to generate a name is to use NSDate to generate a timestamp string and use that as the name of the file. If you use the millisecond precision of the timeinterval functions, you have no risk of getting two files with the same timestamp.

Edit01: From comment:

do u know any method that gets the filename from a given URL

Check NSURL's methods for accessing parts of urls. Remember that urls do not necessarily have a file name. For example, a url that calls a php script does not have a file name associated with it.

In my experience, you should plan on having to generate part of the file name yourself. Unless you have complete control of the creation of the url, you can never ensure that any file name extracted from the url will be unique. If you give two files the same name in the same directory, writing the second file will overwrite the first.

TechZen
Thanks for the reply!actually i do care about the name, to be the same as the url filenamealso i do care to save the file as it is not just raw datait seems we cannot save it directly, so do u know any method that gets the filename from a given URL ?
zanque