I use UIImagePickerController to search for images in my iOS application, and I know that exif information can be obtained using the [UIImagePickerControllerMediaMetadata] information. But when I upload my image to my UIImage server, most of the exif information was striped. I wonder if I can add exif information to my image in the Http request (after that the image loaded as jpg). If not, how can I solve this problem? I want to change the Make, Model attributes (in other words, which device was used for shooting)
Below are my code snippets:
func Tapped() { let myPickerController = UIImagePickerController() myPickerController.delegate = self myPickerController.sourceType = UIImagePickerControllerSourceType.Camera myPickerController.allowsEditing = false self.presentViewController(myPickerController, animated: true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { let image = info[UIImagePickerControllerOriginalImage] as? UIImage myImageView.image = image UIImageWriteToSavedPhotosAlbum(image!, self, #selector(ViewController.image(_:didFinishSavingWithError:contextInfo:)), nil) self.dismissViewControllerAnimated(true, completion: nil) } func myImageUploadRequest() { let myUrl = NSURL(string: "http://XXXXXX/Uploadfile") let request = NSMutableURLRequest(URL:myUrl!) request.HTTPMethod = "POST" let param = [ "userId" : "7" ] let boundary = generateBoundaryString() request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") let imageData = UIImageJPEGRepresentation(myImageView.image!, 1) if(imageData == nil) { return; } request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary) myActivityIndicator.startAnimating() let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { print("error=\(error)") return } // You can print out response object print("******* response = \(response)") // Print out response body let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) print("****** response data = \(responseString!)") do{ let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary }catch{ print(error) } dispatch_async(dispatch_get_main_queue(),{ self.myActivityIndicator.stopAnimating() self.myImageView.image = nil }) } task.resume() } func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData { let body = NSMutableData(); if parameters != nil { for (key, value) in parameters! { body.appendString("--\(boundary)\r\n") body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") body.appendString("\(value)\r\n") } } let filename = "test.jpg" let mimetype = "image/jpg" body.appendString("--\(boundary)\r\n") body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n") body.appendString("Content-Type: \(mimetype)\r\n\r\n") body.appendData(imageDataKey) body.appendString("\r\n") body.appendString("--\(boundary)--\r\n") return body } func generateBoundaryString() -> String { return "Boundary-\(NSUUID().UUIDString)" } extension NSMutableData { func appendString(string: String) { let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) appendData(data!) } }
source share