Created
August 22, 2011 03:25
-
-
Save mtsd/1161592 to your computer and use it in GitHub Desktop.
画像のリサイズ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (CGFloat)aspectRatio:(UIImage *)image size:(CGSize)size | |
| { | |
| CGFloat widthRatio = size.width / image.size.width; | |
| CGFloat heightRatio = size.height / image.size.height; | |
| return (widthRatio < heightRatio) ? widthRatio : heightRatio; | |
| } | |
| - (CGSize)resizedSize:(UIImage *)image size:(CGSize)size | |
| { | |
| CGFloat ratio = [self aspectRatio:image size:size]; | |
| return CGSizeMake(image.size.width*ratio, image.size.height*ratio); | |
| } | |
| /* | |
| UIImage *shrinkedImage = [self resizedImage:image size:CGSizeMake(320, 320)]; | |
| */ | |
| - (UIImage *)resizedImage:(UIImage *)image size:(CGSize)size | |
| { | |
| CGSize resizedSize = [self resizedSize:image size:size]; | |
| UIGraphicsBeginImageContext(resizedSize); | |
| [image drawInRect:CGRectMake(0, 0, resizedSize.width, resizedSize.height)]; | |
| UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| return resizedImage; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| @interface UIImage (Resize) | |
| - (CGFloat)aspectRatio:(CGSize)size; | |
| - (CGSize)resizedSize:(CGSize)size; | |
| - (UIImage *)resizedImage:(CGSize)size; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "UIImage+Resize.h" | |
| @implementation UIImage (Resize) | |
| - (CGFloat)aspectRatio:(CGSize)size | |
| { | |
| CGFloat widthRatio = size.width / self.size.width; | |
| CGFloat heightRatio = size.height / self.size.height; | |
| return (widthRatio < heightRatio) ? widthRatio : heightRatio; | |
| } | |
| - (CGSize)resizedSize:(CGSize)size | |
| { | |
| CGFloat ratio = [self aspectRatio:size]; | |
| return CGSizeMake(self.size.width * ratio, self.size.height * ratio); | |
| } | |
| - (UIImage *)resizedImage:(CGSize)size | |
| { | |
| CGSize resizedSize = [self resizedSize:size]; | |
| UIGraphicsBeginImageContext(resizedSize); | |
| [self drawInRect:CGRectMake(0, 0, resizedSize.width, resizedSize.height)]; | |
| UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| return resizedImage; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment