Cropping CIImage with CICrop isn't working properly

后端 未结 3 1980
说谎
说谎 2020-12-24 03:16

I\'m having troubles cropping image. For me CICrop filter is not working properly. If my CIVector x and y (origins) are 0 everything working fine (image is cropped from left

3条回答
  •  囚心锁ツ
    2020-12-24 04:12

    Are you sure the output image is the size you are expecting? How are you drawing the output image?

    The CICrop filter does not reduce the size of the original image, it just blanks out the content you don't want.

    To get the result you want you probably need to just do this:

    [image drawAtPoint:NSZeroPoint fromRect:NSMakeRect(150, 150, 300, 300) operation:NSCompositeSourceOver fraction:1.0];
    

    If you want an actual CIImage as output rather than just drawing it, just do this:

    CIImage* croppedImage = [image imageByCroppingToRect:CGRectMake(150, 150, 300, 300)];
    
    //you also need to translate the origin   
    CIFilter* transform = [CIFilter filterWithName:@"CIAffineTransform"];
    NSAffineTransform* affineTransform = [NSAffineTransform transform];
    [affineTransform translateXBy:-150.0 yBy:-150.0];
    [transform setValue:affineTransform forKey:@"inputTransform"];
    [transform setValue:croppedImage forKey:@"inputImage"];
    CIImage* transformedImage = [transform valueForKey:@"outputImage"];
    

提交回复
热议问题