Skip to content

Instantly share code, notes, and snippets.

@stillwyw
Created March 9, 2014 04:11
Show Gist options
  • Save stillwyw/9442818 to your computer and use it in GitHub Desktop.
Save stillwyw/9442818 to your computer and use it in GitHub Desktop.
Codes for resizing or cropping a image into proper/wanted size. from http://stackoverflow.com/questions/9894120/uiimageview-fit-to-width
#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
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment