Search code examples
swiftnsurlalamofire

How to upload image with Alamofire from camera roll


I am having trouble uploading image with Alamofire. Problem is very simple - I do not know how get fileURL(NSURL) for a selected image. Here is simple code from Alamofire GitHub:

Alamofire.upload(
                    .POST,
                    "myCustomServerURL",
                    multipartFormData: { multipartFormData in
                        multipartFormData.appendBodyPart(data: "_formname".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"default")
                            //How to get fireURL?
                            multipartFormData.appendBodyPart(fileURL: imageURL, name: "unicorn")
                        },
                        encodingCompletion: { encodingResult in
                            switch encodingResult {
                            case .Success(let upload, _, _):
                                upload.responseJSON { response in
                                    debugPrint(response)
                                }
                            case .Failure(let encodingError):
                                print(encodingError)
                            }
                        }
                    )

What is the usual way to get NSURL for local files?


Solution

  • You get the UIImage instance from the picker. Take the image and write it to disk als jpg (UIImageJPEGRepresentation) or png (UIImagePNGRepresentation) and use the file URL for the store image.