iOS中图片的一些处理,磨砂,压缩,,

2023-12-06 02:08
文章标签 图片 处理 压缩 ios 磨砂

本文主要是介绍iOS中图片的一些处理,磨砂,压缩,,,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

有时候,我们在进行上传图片,或是进行处理图片的时候会出现修改图片大小,进行压缩处理,这时候我们就会用到下面的方法
/**
* 图片压缩处理
*
* @param type 压缩类型(大中小)
*
* @return 压缩后的图片
*/
-(UIImage *) compressionImage:(NSInteger) type;

/**
* 修改图像尺寸并压缩大小
*
* @param image 图片
*
* @return 压缩后的图片
*/
+ (UIImage )scaleAndRotateImage:(UIImage )image;

/**
* 头像图片压缩
*
* @param image 图片
*
* @return 压缩后的图片
*/
+ (UIImage )scaleUserImage:(UIImage )image;

/**
* 将图片做磨砂处理
*
* @param blur 磨砂度
*
* @return
*/
- (UIImage *)blurryImageWithBlurLevel:(CGFloat)blur;
- 这是一个处理图片进行磨砂处理的样子

@implementation UIImage(Category)

/**
* 图片压缩处理
*
* @param type 压缩类型(大中小)
*
* @return 压缩后的图片
*/
-(UIImage *) compressionImage:(NSInteger) type {
CGFloat compressionQuality;
switch (type) {
case image_original:
case image_big:
compressionQuality = 1.0f;
break;
case image_middle:
compressionQuality = 0.8f;
break;
case image_small:
compressionQuality = 0.6f;
break;
default:
compressionQuality = 0;
break;
}
return [UIImage imageWithData:UIImageJPEGRepresentation(self, compressionQuality)];
}

/**
* 等比例压缩
*
* @param sourceImage
* @param size
*
* @return
*/
-(UIImage ) imageCompressForSize:(UIImage )sourceImage targetSize:(CGSize)size{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = size.width;
CGFloat targetHeight = size.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if(CGSizeEqualToSize(imageSize, size) == NO){
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if(widthFactor > heightFactor){
scaleFactor = widthFactor;
}
else{
scaleFactor = heightFactor;
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if(widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}else if(widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}

UIGraphicsBeginImageContext(size);CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();if(newImage == nil){NSLog(@"scale image fail");
}UIGraphicsEndImageContext();return newImage;

}

/**
* 修改图像尺寸并压缩大小
*
* @param image 图片
*
* @return 压缩后的图片
*/
- (UIImage )scaleAndRotateImage:(UIImage )image {
CGSize imageSize = image.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;

CGFloat thumbSize = 0.8;CGFloat tempW = 480;
if (width == 3264 || width == 2592) {tempW = 1024;thumbSize = 0.6;
} else if (width == 2448 || width == 1936) {tempW = 720;thumbSize = 0.6;
} else if (width <= 480) {tempW = screenWidth;
} else if (width > 480 && width <= 1024) {tempW = width;
} else if (width > 1024) {tempW = 1024;
}CGFloat scaleFactor = 0.0;
CGPoint thumbPoint = CGPointMake(0.0,0.0);
CGFloat widthFactor = tempW / width;
CGFloat thumbHeight = tempW * (height/width);
CGFloat heightFactor = thumbHeight / height;if (widthFactor > heightFactor)  {scaleFactor = widthFactor;
}else {scaleFactor = heightFactor;
}CGFloat scaledWidth  = width * scaleFactor;
CGFloat scaledHeight = height * scaleFactor;if (widthFactor > heightFactor) {thumbPoint.y = (thumbHeight - scaledHeight) * 0.5;
} else if(widthFactor < heightFactor) {thumbPoint.x = (tempW - scaledWidth) * 0.5;
}UIGraphicsBeginImageContext(CGSizeMake(tempW, thumbHeight));
CGRect thumbRect = CGRectZero;
thumbRect.origin = thumbPoint;
thumbRect.size.width  = scaledWidth;
thumbRect.size.height = scaledHeight;
[image drawInRect:thumbRect];
UIImage *thumbImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *thumbImageData = UIImageJPEGRepresentation(thumbImage, thumbSize);
CGFloat compressionQuality = thumbSize;while (thumbImageData.length > 280000 && compressionQuality > 0.5) {thumbImageData = UIImageJPEGRepresentation(thumbImage, compressionQuality -= 0.1);
}UIImage *aimage = [UIImage imageWithData: thumbImageData];return aimage;

}

/**
* 头像压缩
*
* @param image 图片
*
* @return 压缩后的图片
*/
+ (UIImage )scaleUserImage:(UIImage )image {
CGSize imageSize = image.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;

CGFloat thumbSize = 0.8;CGFloat tempW = width;if (width > 1024) {tempW = 640;
}CGFloat scaleFactor = 0.0;
CGPoint thumbPoint = CGPointMake(0.0,0.0);
CGFloat widthFactor = tempW / width;
CGFloat thumbHeight = tempW * (height/width);
CGFloat heightFactor = thumbHeight / height;if (widthFactor > heightFactor)  {scaleFactor = widthFactor;
}else {scaleFactor = heightFactor;
}CGFloat scaledWidth  = width * scaleFactor;
CGFloat scaledHeight = height * scaleFactor;if (widthFactor > heightFactor) {thumbPoint.y = (thumbHeight - scaledHeight) * 0.5;
} else if(widthFactor < heightFactor) {thumbPoint.x = (tempW - scaledWidth) * 0.5;
}UIGraphicsBeginImageContext(CGSizeMake(tempW, thumbHeight));
CGRect thumbRect = CGRectZero;
thumbRect.origin = thumbPoint;
thumbRect.size.width  = scaledWidth;
thumbRect.size.height = scaledHeight;
[image drawInRect:thumbRect];
UIImage *thumbImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *thumbImageData = UIImageJPEGRepresentation(thumbImage, thumbSize);
CGFloat compressionQuality = thumbSize;while (thumbImageData.length > 280000 && compressionQuality > 0.5) {thumbImageData = UIImageJPEGRepresentation(thumbImage, compressionQuality -= 0.1);
}UIImage *aimage = [UIImage imageWithData: thumbImageData];return aimage;

}

/**
* 将图片做磨砂处理
*
* @param blur 磨砂度
*
* @return
*/
- (UIImage *)blurryImageWithBlurLevel:(CGFloat)blur {
int boxSize = (int)(blur * 40);
boxSize = boxSize - (boxSize % 2) + 1;

CGImageRef img = self.CGImage;
vImage_Buffer inBuffer, outBuffer;
vImage_Error error;
void *pixelBuffer;//create vImage_Buffer with data from CGImageRef
CGDataProviderRef inProvider = CGImageGetDataProvider(img);
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);inBuffer.width = CGImageGetWidth(img);
inBuffer.height = CGImageGetHeight(img);
inBuffer.rowBytes = CGImageGetBytesPerRow(img);inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);//create vImage_Buffer for output
pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));if(pixelBuffer == NULL)NSLog(@"No pixelbuffer");outBuffer.data = pixelBuffer;
outBuffer.width = CGImageGetWidth(img);
outBuffer.height = CGImageGetHeight(img);
outBuffer.rowBytes = CGImageGetBytesPerRow(img);//perform convolution
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend)
?: vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend)
?: vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);if (error) {NSLog(@"error from convolution %ld", error);
}CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,outBuffer.width,outBuffer.height,8,outBuffer.rowBytes,colorSpace,(CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage *returnImage = [UIImage imageWithCGImage:imageRef];//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);free(pixelBuffer);
//free(pixelBuffer2);
CFRelease(inBitmapData);
CGImageRelease(imageRef);
return returnImage;

}
但是要记得在实现的时候记得倒入两个框架
一#import

impor

这篇关于iOS中图片的一些处理,磨砂,压缩,,的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/459998

相关文章

C#中图片如何自适应pictureBox大小

《C#中图片如何自适应pictureBox大小》文章描述了如何在C#中实现图片自适应pictureBox大小,并展示修改前后的效果,修改步骤包括两步,作者分享了个人经验,希望对大家有所帮助... 目录C#图片自适应pictureBox大小编程修改步骤总结C#图片自适应pictureBox大小上图中“z轴

Go语言使用Buffer实现高性能处理字节和字符

《Go语言使用Buffer实现高性能处理字节和字符》在Go中,bytes.Buffer是一个非常高效的类型,用于处理字节数据的读写操作,本文将详细介绍一下如何使用Buffer实现高性能处理字节和... 目录1. bytes.Buffer 的基本用法1.1. 创建和初始化 Buffer1.2. 使用 Writ

使用Python将长图片分割为若干张小图片

《使用Python将长图片分割为若干张小图片》这篇文章主要为大家详细介绍了如何使用Python将长图片分割为若干张小图片,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果1. Python需求

Python视频处理库VidGear使用小结

《Python视频处理库VidGear使用小结》VidGear是一个高性能的Python视频处理库,本文主要介绍了Python视频处理库VidGear使用小结,文中通过示例代码介绍的非常详细,对大家的... 目录一、VidGear的安装二、VidGear的主要功能三、VidGear的使用示例四、VidGea

Python结合requests和Cheerio处理网页内容的操作步骤

《Python结合requests和Cheerio处理网页内容的操作步骤》Python因其简洁明了的语法和强大的库支持,成为了编写爬虫程序的首选语言之一,requests库是Python中用于发送HT... 目录一、前言二、环境搭建三、requests库的基本使用四、Cheerio库的基本使用五、结合req

使用Python处理CSV和Excel文件的操作方法

《使用Python处理CSV和Excel文件的操作方法》在数据分析、自动化和日常开发中,CSV和Excel文件是非常常见的数据存储格式,ython提供了强大的工具来读取、编辑和保存这两种文件,满足从基... 目录1. CSV 文件概述和处理方法1.1 CSV 文件格式的基本介绍1.2 使用 python 内

如何使用celery进行异步处理和定时任务(django)

《如何使用celery进行异步处理和定时任务(django)》文章介绍了Celery的基本概念、安装方法、如何使用Celery进行异步任务处理以及如何设置定时任务,通过Celery,可以在Web应用中... 目录一、celery的作用二、安装celery三、使用celery 异步执行任务四、使用celery

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

使用 Python 和 LabelMe 实现图片验证码的自动标注功能

《使用Python和LabelMe实现图片验证码的自动标注功能》文章介绍了如何使用Python和LabelMe自动标注图片验证码,主要步骤包括图像预处理、OCR识别和生成标注文件,通过结合Pa... 目录使用 python 和 LabelMe 实现图片验证码的自动标注环境准备必备工具安装依赖实现自动标注核心

Java操作xls替换文本或图片的功能实现

《Java操作xls替换文本或图片的功能实现》这篇文章主要给大家介绍了关于Java操作xls替换文本或图片功能实现的相关资料,文中通过示例代码讲解了文件上传、文件处理和Excel文件生成,需要的朋友可... 目录准备xls模板文件:template.xls准备需要替换的图片和数据功能实现包声明与导入类声明与