#pragma mark -#pragma mark - 缩放处理+ (UIImage *)scaleImage:(UIImage *)image withScale:(float)scale{ UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale)); [image drawInRect:CGRectMake(0, 0, image.size.width * scale, image.size.height * scale)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size { UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height)); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } #pragma mark - #pragma mark - 马赛克处理 static CGContextRef CreateRGBABitmapContext (CGImageRef inImage) { CGContextRef context = NULL; CGColorSpaceRef colorSpace; void *bitmapData; //内存空间的指针,该内存空间的大小等于图像使用RGB通道所占用的字节数。 int bitmapByteCount; int bitmapBytesPerRow; size_t pixelsWide = CGImageGetWidth(inImage); //获取横向的像素点的个数 size_t pixelsHigh = CGImageGetHeight(inImage); bitmapBytesPerRow = (pixelsWide * 4); //每一行的像素点占用的字节数,每个像素点的ARGB四个通道各占8个bit(0-255)的空间 bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); //计算整张图占用的字节数 colorSpace = CGColorSpaceCreateDeviceRGB();//创建依赖于设备的RGB通道 //分配足够容纳图片字节数的内存空间 bitmapData = malloc( bitmapByteCount ); //创建CoreGraphic的图形上下文,该上下文描述了bitmaData指向的内存空间需要绘制的图像的一些绘制参数 context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); //Core Foundation中通过含有Create、Alloc的方法名字创建的指针,需要使用CFRelease()函数释放 CGColorSpaceRelease( colorSpace ); return context; } static unsigned char *RequestImagePixelData(UIImage *inImage) { CGImageRef img = [inImage CGImage]; CGSize size = [inImage size]; //使用上面的函数创建上下文 CGContextRef cgctx = CreateRGBABitmapContext(img); CGRect rect = { { 0,0},{size.width, size.height}}; //将目标图像绘制到指定的上下文,实际为上下文内的bitmapData。 CGContextDrawImage(cgctx, rect, img); unsigned char *data = CGBitmapContextGetData (cgctx); //释放上面的函数创建的上下文 CGContextRelease(cgctx); return data; } + (UIImage *)mosaicImage:(UIImage *)image withLevel:(int)level { unsigned char *imgPixel = RequestImagePixelData(image); CGImageRef inImageRef = [image CGImage]; GLuint width = CGImageGetWidth(inImageRef); GLuint height = CGImageGetHeight(inImageRef); unsigned char prev[4] = { 0}; int bytewidth = width*4; int i,j; int val = level; for(i=0;i