/////////////////////
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 200, 350)];
UIImage *img = [UIImage imageNamed:@"test.jpg"];
imageview.image = [self getThumbnailImgFromImage:img];
[self.view addSubview:imageview];
[imageview release];
}
- (UIImage *)getThumbnailImgFromImage:(UIImage *)image
{
CGSize origImageSize = [image size];
NSLog(@"origImageSize,width=%f",image.size.width);
NSLog(@"origImageSize,height=%f",image.size.height);
CGRect newRect;
newRect.origin = CGPointZero;
newRect.size = CGSizeMake(200, 350);
///缩放比
float ratio = MAX(newRect.size.width/origImageSize.width,
newRect.size.height/origImageSize.height);
///创建位图上下文
UIGraphicsBeginImageContext(newRect.size);
///切个圆角
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:10];
[path addClip];
///新图片的frame ////如果不计算比例,得到的图片是原始图片的裁剪版,不是缩小版。
CGRect thumbnailImag;
thumbnailImag.size.width = ratio * origImageSize.width;
thumbnailImag.size.height = ratio * origImageSize.height;
thumbnailImag.origin.x = (newRect.size.width - thumbnailImag.size.width) / 2.0;
thumbnailImag.origin.y = (newRect.size.height - thumbnailImag.size.height) / 2.0;
///绘图
[image drawInRect:thumbnailImag];
///获取图片
UIImage * small = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"small,width=%f",small.size.width);
NSLog(@"small,height=%f",small.size.height);
/**
*得到的图片是按比例缩小的,如果不按比例缩小,那么直接给一个CGRect,
*他会直接裁剪。可能w h 小裁剪的就是原始图片的部分;
*/
///data
//NSData *data = UIImagePNGRepresentation(small);
//回收内存
UIGraphicsEndImageContext();
return small;
}