NSURLSession limit file size to 100KB

1k Views Asked by At

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];
1

There are 1 best solutions below

2
Rob On

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).