Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Broken on images taken by device's camera in portrait #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.perspectivev3
build

.DS_Store
7 changes: 5 additions & 2 deletions Classes/UIImage+ProportionalFill.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ typedef enum {
MGImageResizeScale // analogous to UIViewContentModeScaleAspectFit, i.e. scale down to fit, leaving space around if necessary.
} MGImageResizingMethod;

- (UIImage *)imageToFitSize:(CGSize)size method:(MGImageResizingMethod)resizeMethod;
//- (UIImage *)imageToFitSize:(CGSize)size method:(MGImageResizingMethod)resizeMethod;
- (UIImage *)imageToFitSize:(CGSize)size method:(MGImageResizingMethod)resizeMethod ignoreScale:(BOOL)ignoreScale;
- (UIImage *)imageCroppedToFitSize:(CGSize)size; // uses MGImageResizeCrop
- (UIImage *)imageScaledToFitSize:(CGSize)size; // uses MGImageResizeScale

@end
- (UIImage *)imageCroppedToFitSize:(CGSize)fitSize ignoreScale:(BOOL)ignoreScale;

@end
21 changes: 16 additions & 5 deletions Classes/UIImage+ProportionalFill.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@implementation UIImage (MGProportionalFill)


- (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resizeMethod
- (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resizeMethod ignoreScale:(BOOL)ignoreScale
{
float imageScaleFactor = 1.0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
Expand Down Expand Up @@ -52,7 +52,8 @@ - (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resize
CGRect sourceRect, destRect;
if (cropping) {
destRect = CGRectMake(0, 0, targetWidth, targetHeight);
float destX, destY;
float destX = 0;
float destY = 0;
if (resizeMethod == MGImageResizeCrop) {
// Crop center
destX = round((scaledWidth - targetWidth) / 2.0);
Expand Down Expand Up @@ -92,8 +93,13 @@ - (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resize

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
CGImageRef sourceImg = nil;
if (self.imageOrientation == UIImageOrientationRight || self.imageOrientation == UIImageOrientationLeft)
{
sourceRect = CGRectMake(sourceRect.origin.y, sourceRect.origin.x, sourceRect.size.height, sourceRect.size.width);
}
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(destRect.size, NO, 0.f); // 0.f for scale means "scale for device's main screen".
float newScale = (ignoreScale) ? 1.0f : 0;
UIGraphicsBeginImageContextWithOptions(destRect.size, NO, newScale); // 0.f for scale means "scale for device's main screen".
sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect); // cropping happens here.
image = [UIImage imageWithCGImage:sourceImg scale:0.0 orientation:self.imageOrientation]; // create cropped UIImage.

Expand Down Expand Up @@ -128,15 +134,20 @@ - (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resize
}


- (UIImage *)imageCroppedToFitSize:(CGSize)fitSize ignoreScale:(BOOL)ignoreScale
{
return [self imageToFitSize:fitSize method:MGImageResizeCrop ignoreScale:ignoreScale];
}

- (UIImage *)imageCroppedToFitSize:(CGSize)fitSize
{
return [self imageToFitSize:fitSize method:MGImageResizeCrop];
return [self imageToFitSize:fitSize method:MGImageResizeCrop ignoreScale:FALSE];
}


- (UIImage *)imageScaledToFitSize:(CGSize)fitSize
{
return [self imageToFitSize:fitSize method:MGImageResizeScale];
return [self imageToFitSize:fitSize method:MGImageResizeScale ignoreScale:FALSE];
}


Expand Down
2 changes: 1 addition & 1 deletion Classes/UIImage+Tint.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ - (UIImage *)imageTintedWithColor:(UIColor *)color fraction:(CGFloat)fraction

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.f); // 0.f for scale means "scale for device's main screen".
UIGraphicsBeginImageContextWithOptions([self size], NO, self.scale); // 0.f for scale means "scale for device's main screen".
} else {
UIGraphicsBeginImageContext([self size]);
}
Expand Down