1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| + (void) resize_picture:(UIImage*) picture save_in:(UIImage**) resized_picture with_width:(float) max_picture_width and_height:(float) max_picture_height {
if (picture == nil) { return; }
RELEASE( (*resized_picture) );
float new_width = 0.0f, new_height = 0.0f;
float width = (picture.size.width * picture.scale);
float height = (picture.size.height * picture.scale);
float h_scale = 0.0f, w_scale = 0.0f;
float max_width = (float) (max_picture_width * 0.8f);
float max_height = (float) (max_picture_height * 0.8f);
if ((width < max_width) && (height < max_height)) {
// (*resized_picture) = [[UIImage alloc] initWithCGImage:picture.CGImage];
if (width > height) {
new_width = max_width;
h_scale = (new_width / width);
new_height = (height * h_scale);
} else {
new_height = max_height;
w_scale = (new_height / height);
new_width = (width * w_scale);
}
} else {
w_scale = (float) (max_width / width);
h_scale = (float) (max_height / height);
if (h_scale < w_scale) {
new_width = (h_scale * width);
new_height = (h_scale * height);
} else {
new_width = (w_scale * width);
new_height = (w_scale * height);
}
}
DLOG( (@"Utils::resize_picture - From (%f, %f) to (%f, %f) [%f]", width, height, new_width, new_height, picture.scale) )
NSAutoreleasePool* imagePool = [[NSAutoreleasePool alloc] init];
CGRect resizedRect = CGRectIntegral( CGRectMake(0, 0, new_width, new_height) );
CGImageRef imageRef = picture.CGImage;
// CGContextRef bitmap = CGBitmapContextCreate(nil, new_width, new_height, CGImageGetBitsPerComponent(imageRef),
// 0, CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef));
CGContextRef bitmap = CGBitmapContextCreate(nil, new_width, new_height, 8, 0, CGImageGetColorSpace(imageRef), kCGImageAlphaPremultipliedLast);
CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
CGContextDrawImage(bitmap, resizedRect, imageRef);
CGImageRef resizedImageRef = CGBitmapContextCreateImage(bitmap);
(*resized_picture) = [[UIImage alloc] initWithCGImage:resizedImageRef];
/* Clean up */
CGContextRelease(bitmap);
CGImageRelease(resizedImageRef);
[imagePool release];
} |
Partager