I have a UIImageView
that is housed inside a UIScrollView
. The image in the image view is bigger than the iPhone's screen. What I want to do is have the image proportionately scaled to fit the width of the screen without changing the aspect ratio of the image (part of the image would be offscreen on the bottom).
I've tried using UIViewContentModeScaleAspectFill
and UIViewContentModeScaleAspectFit
but neither does what I want.
Any ideas?
Thanks.
Here's an image class I wrote to do some simple functions like this, fitInsideWidth is the function that should apply here:
MyImage.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface MyImage : NSObject
+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width covertToHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image convertToHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width;
+ (UIImage*)imageWithImage:(UIImage *)image fitInsideWidth:(float)width fitInsideHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image fitOutsideWidth:(float)width fitOutsideHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image cropToWidth:(float)width cropToHeight:(float)height;
@end
MyImage.m
#import "MyImage.h"
@implementation MyImage
+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width covertToHeight:(float)height {
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}
+ (UIImage*)imageWithImage:(UIImage *)image convertToHeight:(float)height {
float ratio = image.size.height / height;
float width = image.size.width / ratio;
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}
+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width {
float ratio = image.size.width / width;
float height = image.size.height / ratio;
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}
+ (UIImage*)imageWithImage:(UIImage *)image fitInsideWidth:(float)width fitInsideHeight:(float)height {
if (image.size.height >= image.size.width) {
return [MyImage imageWithImage:image convertToWidth:width];
} else {
return [MyImage imageWithImage:image convertToHeight:height];
}
}
+ (UIImage*)imageWithImage:(UIImage *)image fitOutsideWidth:(float)width fitOutsideHeight:(float)height {
if (image.size.height >= image.size.width) {
return [MyImage imageWithImage:image convertToHeight:height];
} else {
return [MyImage imageWithImage:image convertToWidth:width];
}
}
+ (UIImage*)imageWithImage:(UIImage *)image cropToWidth:(float)width cropToHeight:(float)height {
CGSize size = [image size];
CGRect rect = CGRectMake(((size.width-width) / 2.0f), ((size.height-height) / 2.0f), width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage * img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With