OC 使用CIFilter添加图片

+(UIImage *)createInviteImgWithUserCodeStr:(NSString *)userCode{
    userCode = [NSString stringWithFormat:@"https://v.16mnc.cn:9999/neighbour-goods/inviteRegist.html?userCode=%@",userCode];
    NSString *imagePath = [PublicService getDirectoryWithFile:userCode];
    NSFileManager *fm = [NSFileManager defaultManager];
    if([fm fileExistsAtPath:imagePath]){
        return [UIImage imageWithContentsOfFile:imagePath];
    }
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSData *data = [userCode dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];              //通过kvo方式给一个字符串,生成二维码
    [filter setValue:@"H" forKey:@"inputCorrectionLevel"];      //设置二维码的纠错水平,纠错水平越高,可以污损的范围越大
    
    //设置背景颜色和填充颜色 默认白色背景黑色填充
    
    CIColor *color1 = [CIColor colorWithCGColor:[UIColor blackColor].CGColor];
    CIColor *color2 = [CIColor colorWithCGColor:[UIColor whiteColor].CGColor];
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: filter.outputImage ,@"inputImage",
                                color1,@"inputColor0",
                                color2,@"inputColor1",nil];
    CIFilter *newFilter = [CIFilter filterWithName:@"CIFalseColor" withInputParameters:parameters];
    UIImage *img = [PublicService createNonInterpolatedUIImageFormCIImage:[newFilter outputImage] withSize:SCREEN_WIDTH];
    
    
    
    //把图片直接保存到指定的路径(同时应该把图片的路径imagePath存起来,下次就可以直接用来取)
    [UIImagePNGRepresentation(img) writeToFile:imagePath atomically:YES];
    
    return img;
}


/**
 * 调用该方法处理图像变清晰
 * 根据CIImage生成指定大小的UIImage
 *
 * @param image CIImage
 * @param size 图片宽度以及高度
 */
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    CGRect extent = CGRectIntegral(image.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:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    //原图
    UIImage *outputImage = [UIImage imageWithCGImage:scaledImage];
    UIGraphicsBeginImageContextWithOptions(outputImage.size, NO, [[UIScreen mainScreen] scale]);
    [outputImage drawInRect:CGRectMake(0,0 , size, size)];
    //水印图
    UIImage *waterimage = [UIImage imageNamed:@"icon"];
    [waterimage drawInRect:CGRectMake((outputImage.size.height-(outputImage.size.height)*0.3)*0.5, (outputImage.size.height-(outputImage.size.height)*0.3)*0.5, (outputImage.size.height)*0.3, (outputImage.size.height)*0.3)];
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newPic;
}

 

posted @ 2019-03-30 14:22  新年新气象  阅读(419)  评论(0编辑  收藏  举报