本文主要是介绍OS 图片压缩器 输出不同分辨率图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://github.com/KillWilde/CYImageCompressor
主要代码
#pragma mark 压缩图片到对应尺寸
-(NSImage *)compressImage:(NSImage *)sourceImg withSize:(CGSize)size
{
// convert NSImage to bitmap
NSData * tiffData = [sourceImg TIFFRepresentation];
NSBitmapImageRep * bitmap;
bitmap = [NSBitmapImageRep imageRepWithData:tiffData];
// create CIImage from bitmap
CIImage * ciImage = [[CIImage alloc] initWithBitmapImageRep:bitmap];
float width = CGImageGetWidth(ciImage.CGImage);
float height = CGImageGetHeight(ciImage.CGImage);
CGImageRef ref = CGImageCreateWithImageInRect(ciImage.CGImage, CGRectMake(0, 0, width, height));
NSRect imageRect = NSMakeRect(0.0, 0.0, size.width, size.height);
CGContextRef imageContext = nil;
NSImage* newImage = nil;
// Create a new image to receive the Quartz image data.
newImage = [[NSImage alloc] initWithSize:imageRect.size];
[newImage lockFocus];
// Get the Quartz context and draw.
imageContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextDrawImage(imageContext, *(CGRect*)&imageRect, ref);
[newImage unlockFocus];
return newImage;
}
这篇关于OS 图片压缩器 输出不同分辨率图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!