【iOS开发笔记24/50】调整UIImage的大小
2012-06-22 21:02
501 查看
对UIImage调整大小,不知道苹果的API里为什么不提供?
//
// ImageExtras.h
//
#import <Foundation/Foundation.h>
@interface UIImage (Extras)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize;
@end
//
// ImageExtras.m
//
// 摘自下面的论坛
// http://www.iphonedevsdk.com/forum/iphone-sdk-development/7307-resizing-photo-new-uiimage-2.html
//
#import "ImageExtras.h"
@implementation UIImage (Extras)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize {
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
return newImage ;
}
@end
//
// ImageExtras.h
//
#import <Foundation/Foundation.h>
@interface UIImage (Extras)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize;
@end
//
// ImageExtras.m
//
// 摘自下面的论坛
// http://www.iphonedevsdk.com/forum/iphone-sdk-development/7307-resizing-photo-new-uiimage-2.html
//
#import "ImageExtras.h"
@implementation UIImage (Extras)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize {
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
return newImage ;
}
@end
相关文章推荐
- iOS开发笔记-根据frame大小动态调整fontSize的自适应文本及圆形进度条控件的实现
- 【iOS开发笔记23/50】使用ARC时,如果不加strong,有时会给出警告
- 【iOS开发笔记20/50】Tap手势
- 【iOS开发笔记25/50】:正则表达式
- #iOS开发笔记#如何限制UIImageView放大缩小的比例
- iPhone开发笔记[6/50]-applicationDidFinishLaunching是iOS3.0以前用的方法
- IOS开发学习笔记024-UIButton和UIImageView的区别
- iOS开发:UIImage 图片处理:截图,缩放,设定大小,存储
- iOS开发笔记[19/50]:集成iAd广告
- #iOS开发笔记#UIImageView实现拖动,放大/缩小,旋转
- iOS开发笔记[16/50]:Views
- iOS开发7:自动旋转与调整大小
- ios开发学习笔记--调用相册或相机(UIImagePickerController)
- iOS开发7:自动旋转与调整大小
- Web开发笔记(3):Eclipse中调整JSP字体大小
- [Swift]iOS开发: 图片翻转、切割圆角、调整大小
- iOS开发笔记[18/50]:在Mac OS X Lion系统中访问~/Library目录都需要点技巧
- iOS开发笔记--异步加载UIImageView----AsyImageView
- iOS开发笔记[13/50]:解决SenTestingKit/SenTestingKit.h: No such file or directory问题
- 【iOS开发笔记21/50】获取应用程序的名称和版本号