1.当某个视图的大小发生变化时,这个视图会收到layoutSubviews消息。
2.创建缩略图:
- (void)setThumbnailDataFromImage:(UIImage *)image
{
CGSize origImageSize = [image size];
CGRect newRect;
newRect.origin = CGPointZero;
newRect.size = [[self class] thumbnailSize];
//确定缩放倍数
float ratio = MAX(newRect.size.width/origImageSize.width, newRect.size.height/origImageSize.height);
//创建位图上下文
UIGraphicsBeginImageContext(newRect.size);
//加上圆角效果
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];
[path addClip];
//确定制图的矩形区域
CGRect projectRect;
projectRect.size.width = ratio * origImageSize.width;
projectRect.size.height = ratio * origImageSize.height;
projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;
projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;
//在矩形中制图
[image drawInRect:projectRect];
//通过图片上下文得到UIImage对象,赋给thumbnail属性并保留它
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
[self setthumbnail:smallImage];
//获取图片的PNG格式数据
NSData *data = UIImagePNGRepresentation(smallImage);
[self setThumbnailData:data];
//清理图片上下文
UIGraphicsEndImageContext();
}