Justin Erswell
Justin Erswell

Reputation: 708

iOS imageWithData resize to 640 x 480

I have an iOS method written to upload a chosen image to a web server thus:

NSData *imageData = UIImagePNGRepresentation(imageView.image);
NSString *urlString = @"http://awebserversomewher.com/upload.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"%@",returnString);

Before this image is submitted however i would like to resize it to make it easier and quicker to upload can any on show me how?

Thanks

Justin

Upvotes: 2

Views: 2640

Answers (2)

Richard J. Ross III
Richard J. Ross III

Reputation: 55563

Here is a way you can do it to end up with whatever size you want (doesn't necessarily keep scale, which is a pro and a con):

UIImage *UIImageResize(UIImage *image, CGSize targetSize)
{
    UIGraphicsBeginImageContext(targetSize);
    CGContextRef bitmapContext = UIGraphicsGetCurrentContext();

    CGContextScaleCTM(bitmapContext, 1, -1);
    CGContextDrawImage(bitmapContext, (CGRect) { .origin.y = -targetSize.height, .size = targetSize }, image.CGImage);

    UIImage *results = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return results;
}

Example usage:

UIImage *resizedImage = UIImageResize([UIImage imageNamed:@"NukeBusters.png"], CGSizeMake(125, 130));
imageView.image = resizedImage;

Upvotes: 3

Erpheus
Erpheus

Reputation: 826

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:scale orientation:original.imageOrientation];

If you want a pre defined size for the image just calculate the scale yourself.

For example, if you want to limit width then do

scale = desiredWidth / imageWidth

If you want to limit both then

scale1 = desiredWidth / imageWidth
scale2 = desiredHeight / imageHeight
scale = min (scale1 , scale2 )

Upvotes: 2

Related Questions