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