12345678910111213141516171819202122232425262728293031323334353637383940 |
- //
- // CIImage+YHExtend.m
- // YouHuiProject
- //
- // Created by jcymac on 2018/5/19.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "CIImage+YHExtend.h"
- @implementation CIImage (YHExtend)
- /**
- * 根据CIImage生成指定大小的UIImage
- *
- * @param size 图片宽度
- */
- - (UIImage *)createNonInterpolatedUIImagewithSize:(CGFloat) size
- {
- CGRect extent = CGRectIntegral(self.extent);
- CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
- // 1.创建bitmap;
- size_t width = CGRectGetWidth(extent) * scale;
- size_t height = CGRectGetHeight(extent) * scale;
- CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
- CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
- CIContext *context = [CIContext contextWithOptions:nil];
- CGImageRef bitmapImage = [context createCGImage:self fromRect:extent];
- CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
- CGContextScaleCTM(bitmapRef, scale, scale);
- CGContextDrawImage(bitmapRef, extent, bitmapImage);
- // 2.保存bitmap到图片
- CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
- CGContextRelease(bitmapRef);
- CGImageRelease(bitmapImage);
- return [UIImage imageWithCGImage:scaledImage];
- }
- @end
|