I am capturing image from camera and converting image into base64 format. It takes more time to send images to server. If there is no internet connection, i am storing it into local DB and once i get internet connection, i want to send multiple images to server. What is the best way to send images to server from app.
Asked
Active
Viewed 1,060 times
0
-
Sending image through base64 is not advisable, it takes lot of time to get uploaded. Use Multipart for uploading. Go through this link http://stackoverflow.com/questions/29623187/upload-image-with-multipart-form-data-ios-in-swift – Madhu Jul 22 '16 at 05:57
-
You can send images by using multipart format. – Vishal Sonawane Jul 22 '16 at 06:08
2 Answers
1
Have you ever tried Alamofire ? It supports file upload.
Here is an example for image load:
public func requestImage(url: String) -> SignalProducer<UIImage, NetworkError> {
return SignalProducer { observer, disposable in
let serializer = Alamofire.Request.dataResponseSerializer()
Alamofire.request(.GET, url)
.response(queue: self.queue, responseSerializer: serializer) {
response in
switch response.result {
case .Success(let data):
guard let image = UIImage(data: data) else {
observer.sendFailed(.IncorrectDataReturned)
return
}
observer.sendNext(image)
observer.sendCompleted()
case .Failure(let error):
observer.sendFailed(NetworkError(error: error))
}
}
}
}
And this one of the example for async image load.

firats
- 496
- 2
- 7
-1
You can make request for multipart as follows and then use that request in your NSURLSession
NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
// configure the request
NSString *urlString = YOUR_URL;
NSURL *url =[NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
// set content type
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
//create body
request.HTTPBody = [self createBodyWithParameters:params paths:@[filePath] fieldName:fieldName boundary:boundary]; //params(NSDictionary) WILL BE YOUR PARAMETERS TO WEB SERVICE
[request setValue:[NSString stringWithFormat:@"%lu",(long)[request.HTTPBody length]] forHTTPHeaderField:@"Content-Lenght"];

Vishal Sonawane
- 2,637
- 2
- 16
- 21
-
This question is tagged "swift". Your solution may have value, but is not written in the language requested by OP, so it's off-topic. Please provide an answer in Swift. Thank you. – Eric Aya Jul 22 '16 at 07:53