缩略图片

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();

    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值