image等比缩小

来源:互联网 发布:新网互联域名如何续费 编辑:程序博客网 时间:2024/06/02 13:31

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth//图片压缩

{

   CGSize imageSize = sourceImage.size;

   CGFloat width = imageSize.width;

   CGFloat height = imageSize.height;

   CGFloat targetWidth = defineWidth;

   CGFloat targetHeight = (targetWidth / width) * height;

    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));

    [sourceImagedrawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];

    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

   return newImage;

}

0 0
原创粉丝点击