图片压缩

//图片大小缩放
+ (UIImage *) scaleImage:(UIImage *)image toSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);          //size 为CGSize类型,即你所需要的图片尺寸
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;   //返回的就是已经改变的图片
}

 //压缩图片,并返回成数据格式的
+ (NSData *) zipImage:(UIImage *)img rate:(CGFloat)rate{
    NSData *retImg = UIImageJPEGRepresentation(img, 0.5);
    if (retImg==nil) {
        retImg = UIImagePNGRepresentation(img);
    }
    return retImg;
} 

 

posted @ 2015-11-20 16:58  TheYouth  阅读(200)  评论(0编辑  收藏  举报