Aditya Dharma
Aditya Dharma

Reputation: 718

Get filename UIImagePickerController in swift

How do you get the filename of the picture taken from the camera with swift? I need the filename because I want to get the path and send it to the web server.

let path = NSBundle.mainBundle().pathForResource("imageName", ofType: "jpg") as String

Thanks.

Upvotes: 1

Views: 2020

Answers (1)

Gutty1
Gutty1

Reputation: 535

Please allow me to sort your question and offer different alternatives:

When you take pictures with an iOS device by using the UIImagePickerController or any other available controller, the image is usually saved in a UIImage object and not necessary to the bundle documents folder in the app, unless you decide to do it in the code for further display. Regarding uploading the image to a web server, it depends on the destination server requirements, therefore, you should check what are the methods available for you to upload the image to that server. I'm familiar with few options as follow:

1) The destination server requires to deliver the image in multipart/form-data, then you should convert your UIImage to NSData by using one to the two available methods UIImageJPEGRepresentation or UIImagePNGRepresentation depending in which format you are required to send. I suggest you check how to build the call to the destination server method (there are several post in StackOverflow about it)

2) Server requires to deliver the image in base64EncodedString mode, then you need to convert your image as in the following example:

let string: String = "data:image/png;base64," + (UIImagePNGRepresentation(image!)?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength))!

3)The destination server requires a path (meaning they don't store the image): you should first save the picture in an accessible path in the web (not inside the iOS device) by probably using one of the 2 methods I just described above and then, upload the image path you get from the place you uploaded the picture to the destination server.

I hope I succeeded to help you with my answer :-)

Upvotes: 1

Related Questions