以下是筆記,這是蠻常用到的功能所以我把他加到Category裡面方便可以重複使用
建立一個新的項目選擇Category ,名稱輸入Resize類別選擇UIImage
UIImage+Resize.h
@interface UIImage (Resize)
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
@end
記得是宣告為靜態方法
UIImage+Resize.m
#import "UIImage+Resize.h"
@implementation UIImage (Resize)
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return newImage; }
@end
使用UIGraphicsBeginImageContext需加入
#import <QuartzCore/QuartzCore.h>
使用方法
UIImage *popimg = [UIImage imageWithImage:[UIImage imageNamed:@"xxx.png"] scaledToSize:CGSizeMake(50, 50)];
沒有留言:
張貼留言