2

我正在使用 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];

// 新图像尺寸更大,大部分被裁剪:-/

4

1 回答 1

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];

在您的代码中,您正在从矩形创建 CGImage。你没有使用.extent. 这会导致图像的原始尺寸丢失。

于 2014-01-17T06:19:55.997 回答