1 | /* |
---|
2 | * PhoneGap is available under *either* the terms of the modified BSD license *or* the |
---|
3 | * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. |
---|
4 | * |
---|
5 | * Copyright (c) 2005-2010, Nitobi Software Inc. |
---|
6 | * Copyright (c) 2011, IBM Corporation |
---|
7 | * Copyright (c) 2011, Ambrose Software, Inc |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | #import "cameraExtended.h" |
---|
12 | |
---|
13 | @implementation PGCameraExtended |
---|
14 | |
---|
15 | |
---|
16 | - (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize |
---|
17 | { |
---|
18 | UIImage *sourceImage = anImage; |
---|
19 | UIImage *newImage = nil; |
---|
20 | CGSize imageSize = sourceImage.size; |
---|
21 | CGFloat width = imageSize.width; |
---|
22 | CGFloat height = imageSize.height; |
---|
23 | CGFloat targetWidth = targetSize.width; |
---|
24 | CGFloat targetHeight = targetSize.height; |
---|
25 | CGFloat scaledWidth = targetWidth; |
---|
26 | CGFloat scaledHeight = targetHeight; |
---|
27 | |
---|
28 | CGFloat ratio_width = width / targetWidth; |
---|
29 | CGFloat ratio_height = height / targetHeight; |
---|
30 | |
---|
31 | // maximal size exceeded ? |
---|
32 | if (ratio_width > 1 || ratio_height > 1) |
---|
33 | { |
---|
34 | if (ratio_width < ratio_height) |
---|
35 | { |
---|
36 | scaledWidth = ceil(width / ratio_height); |
---|
37 | } |
---|
38 | else |
---|
39 | { |
---|
40 | scaledHeight = ceil(height / ratio_width); |
---|
41 | } |
---|
42 | |
---|
43 | targetSize = CGSizeMake(scaledWidth, scaledHeight); |
---|
44 | self.pickerController.targetSize = targetSize; |
---|
45 | } |
---|
46 | else |
---|
47 | { |
---|
48 | return sourceImage; |
---|
49 | } |
---|
50 | |
---|
51 | UIGraphicsBeginImageContext(targetSize); |
---|
52 | |
---|
53 | CGRect thumbnailRect = CGRectZero; |
---|
54 | thumbnailRect.size.width = scaledWidth; |
---|
55 | thumbnailRect.size.height = scaledHeight; |
---|
56 | |
---|
57 | [sourceImage drawInRect:thumbnailRect]; |
---|
58 | |
---|
59 | newImage = UIGraphicsGetImageFromCurrentImageContext(); |
---|
60 | if(newImage == nil) |
---|
61 | NSLog(@"could not scale image"); |
---|
62 | |
---|
63 | //pop the context to get back to the default |
---|
64 | UIGraphicsEndImageContext(); |
---|
65 | return newImage; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | @end |
---|