简体   繁体   中英

UIImagePNGRepresentation returns nil after CIFilter

Running into a problem getting a PNG representation for a UIImage after having rotated it with CIAffineTransform. First, I have a category on UIImage that rotates an image 90 degrees clockwise. It seems to work correctly when I display the rotated image in a UIImageView.

-(UIImage *)cwRotatedRepresentation
{
    // Not very precise, stop yelling at me.
    CGAffineTransform xfrm=CGAffineTransformMakeRotation(-(6.28 / 4.0));
    CIContext *context=[CIContext contextWithOptions:nil];
    CIImage *inputImage=[CIImage imageWithCGImage:self.CGImage];
    CIFilter *filter=[CIFilter filterWithName:@"CIAffineTransform"];
    [filter setValue:inputImage forKey:@"inputImage"];
    [filter setValue:[NSValue valueWithBytes:&xfrm objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

    CIImage *result=[filter valueForKey:@"outputImage"];
    CGImageRef cgImage=[context createCGImage:result fromRect:[inputImage extent]];

    return [[UIImage alloc] initWithCIImage:result];

}

However, when I try to actually get a PNG for the newly rotated image, UIImagePNGRepresentation returns nil.

-(NSData *)getPNG
{
    UIImage *myImg=[UIImage imageNamed:@"canada"];
    myImg=[myImg cwRotatedRepresentation];
    NSData *d=UIImagePNGRepresentation(myImg);

    // d == nil :(
    return d;
}

Is core image overwriting the PNG headers or something? Is there a way around this behavior, or a better means of achieving the desired result of a PNG representation of a UIImage rotated 90 degrees clockwise?

Not yelling, but -M_PI_4 will give you the constant you want with maximum precision :)

The only other thing that I see is you probably want to be using [result extent] instead of [inputImage extent] unless your image is known square.

Not sure how that would cause UIImagePNGRepresentation to fail though. One other thought... you create a CGImage and then use the CIImage in the UIImage, perhaps using initWithCGImage would give better results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM