/* * PhoneGap is available under *either* the terms of the modified BSD license *or* the * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. * * Copyright (c) 2005-2010, Nitobi Software Inc. * Copyright (c) 2011, IBM Corporation * Copyright (c) 2011, Ambrose Software, Inc * */ #import "cameraExtended.h" @implementation PGCameraExtended - (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize { UIImage *sourceImage = anImage; UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = targetSize.width; CGFloat targetHeight = targetSize.height; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGFloat ratio_width = width / targetWidth; CGFloat ratio_height = height / targetHeight; // maximal size exceeded ? if (ratio_width > 1 || ratio_height > 1) { if (ratio_width < ratio_height) { scaledWidth = ceil(width / ratio_height); } else { scaledHeight = ceil(height / ratio_width); } targetSize = CGSizeMake(scaledWidth, scaledHeight); self.pickerController.targetSize = targetSize; } else { return sourceImage; } UIGraphicsBeginImageContext(targetSize); CGRect thumbnailRect = CGRectZero; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil) NSLog(@"could not scale image"); //pop the context to get back to the default UIGraphicsEndImageContext(); return newImage; } @end