我正在使用CIPixellate在ios6中对像素进行像素化处理。它会产生更大的图像。如何保持原始尺寸?
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(capturedImageView.image, 1.0)];//[NSData dataWithData:UIImagePNGRepresentation(capturedImageView.image)];
beginImage = [CIImage imageWithData:imageData];
context = [CIContext contextWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:kCIContextUseSoftwareRenderer]];
filter = [CIFilter filterWithName:@"CIPixellate" keysAndValues:kCIInputImageKey, beginImage, @"inputScale", [NSNumber numberWithFloat:14.0], nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:CGRectMake(0, 0, 320, 480)];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
//新图片尺寸较大,大部分都被剪裁: - /
答案 0 :(得分:3)
UIImageOrientation originalOrientation = origim.imageOrientation;
CIImage *result = [CIImage imageWithCGImage:origim.CGImage];
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *pixel = [CIFilter filterWithName:@"CIPixellate"];
[pixel setDefaults];
[pixel setValue:result forKey:@"inputImage"];
[pixel setValue:[CIVector vectorWithX:100.0 Y:100.0] forKey:kCIInputCenterKey];
[pixel setValue:[NSNumber numberWithFloat:yeslider.value] forKey:kCIInputScaleKey];
result = [pixel valueForKey:kCIOutputImageKey];
CGImageRef imgRef = [context createCGImage:result fromRect:result.extent];
UIImage *newImage = [UIImage imageWithCGImage:imgRef];
在你的代码中,你是从rect创建CGImage。您没有使用.extent
。这会导致图像的原始尺寸丢失。