#pragma mark - 获取按尺寸压缩过的新图片 + (UIImage *)getNewImageWithImage:(UIImage *)image size:(CGFloat)tempSize { //获取图片的宽高 CGSize imageSize = image.size; CGFloat imgH = imageSize.height; CGFloat imgW = imageSize.width; if (imgH == imgW) {//如果是正方形 imgW = tempSize; imgH = tempSize; }else if(imgW > tempSize ){//如果宽度大于200/600 //获取图像宽度的倍数 CGFloat multiple = imgW / tempSize; imgW = tempSize; imgH = imgH / multiple; }else if(imgH > tempSize ){//如果高度大于200/600 //获取图像高度的倍数 CGFloat multiple = imgH / tempSize; imgH = tempSize; imgW = imgW / multiple; } //重新赋值图片的宽高 imageSize.width = imgW; imageSize.height = imgH; //压缩新图片 UIGraphicsBeginImageContext(imageSize); [image drawInRect:CGRectMake(kZero, kZero, imageSize.width, imageSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
时间: 2024-10-26 01:50:33