0

I am unsure what is happening but I have a code that sends an image to a server running on my Mac. It works perfectly when I set the UIImageJPEGRepresentation to 0.1 quality. But anything above that (like below) will crash the print server. It seems like the maximum file size is 100KB because that is the file size when the quality is 0.1.

UIImage *newImage = [UIImage imageNamed:@"Movies.jpg"];

NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *printip = [defaults valueForKey:@"printip"];
NSString * valueToSave1 = [NSString stringWithFormat:@"http://%@:45500", printip];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[imageData length]];
NSLog (@"POST LENGTH: %@", postLength);


NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.discretionary = false;
sessionConfiguration.timeoutIntervalForRequest = 30.0;
sessionConfiguration.timeoutIntervalForResource = 60.0;
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

// Create the session
// We can use the delegate to track upload progress
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

// Data uploading task. We could use NSURLSessionUploadTask instead of NSURLSessionDataTask if we needed to support uploads in the background
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:valueToSave1]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:imageData];

NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // Process the response
}];
[uploadTask resume];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Marc Matteo
  • 103
  • 2
  • 9
  • Nothing to do with iOS or NSURLSessionConfiguration. If there is a very small file size limit (unlikely) that is between you and the server. – matt Dec 08 '19 at 17:55

1 Answers1

0

There are a few possible issues:

  1. Your server code simply might just be configured to not permit uploads that exceed a particular size. That’s not an uncommon practice (to avoid excessive storage usage, images that are too big, prevent bad actors from uploading other large payloads, etc.).

  2. Your request could be timing out because it’s taking too long to upload this larger asset.

  3. You could be running out of memory when creating requests where you pre-populate the httpBody with the data of the image. You can reduce this peak memory usage by using uploadtaskwithrequest:fromFile:completionHandler:. This reduces the peak memory usage when uploading.

Option 1 is the most likely issue, but we shouldn't have to guess. Upload an asset greater than 100kb and look at the error code or the body of the response. That will tell you precisely what’s going wrong.


Assuming you end up needing to reduce the asset size, it’s worth noting that a JPEG with quality of 0.1 will often be seriously degraded with all sorts of JPEG “artifacts”. I’d personally stick with quality values of 0.7 or so, and if the asset is still too large, reduce the image dimensions. For example, rather than 3000×2000 image with 0.1, I might reduce to 1200×800 with quality of 0.7 (or whatever).

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for your response. The above code works perfectly in iOS12 and sends photos up to 6MB with no issue. On iOS13 the server crashes. It's not timing out and its not running out of memory. There's something different with iOS13 I feel – Marc Matteo Dec 09 '19 at 12:03
  • I should also note that when I restart the server after it crashes the photo is in the folder. So it does get transferred completely but something is causing the crash. – Marc Matteo Dec 09 '19 at 12:18