ios对图像举行压缩代码

By admin in 4858.com on 2019年6月29日
- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
 CGSize imageSize = image.size;
 CGFloat width = imageSize.width;
 CGFloat height = imageSize.height;

 if (width <= newSize.width && height <= newSize.height){
  return image;
 }

 if (width == 0 || height == 0){
  return image;
 }

 CGFloat widthFactor = newSize.width / width;
 CGFloat heightFactor = newSize.height / height;
 CGFloat scaleFactor = (widthFactor<heightFactor?widthFactor:heightFactor);

 CGFloat scaledWidth = width * scaleFactor;
 CGFloat scaledHeight = height * scaleFactor;
 CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);

    UIGraphicsBeginImageContext(targetSize);
    [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有