CGImage from NSURL works but not from UIImage

后端 未结 2 627
Happy的楠姐
Happy的楠姐 2021-01-26 10:48

I have some issues applying filters to an image. The following code works perfect when using CIImage:imageWithContentsOfURL:

NSInteger filterIndex = [(UITapGestu         


        
2条回答
  •  悲哀的现实
    2021-01-26 11:12

    The problem is that

    [UIImage imageNamed:@"test.png"].CIImage
    

    doesn't do what you think it does. It doesn't convert a UIImage into a CIImage, or derive a CIImage from a UIImage; it merely provides a reference to the CIImage that is already the basis of this UIImage. But this UIImage does not have a CIImage basis; it is a bitmap (CGImage), not a filter output (CIImage).

    That is why, as you have rightly discovered, you have use a different method, CIImage's initWithCGImage:, which does perform the conversion. The UIImage (as I just said) does have a CGImage basis, so this works.

    In other words, a UIImage is (mostly) just a wrapper. It can be a wrapper for a bitmap (CGImage) or for a filter output (CIImage). And those two methods, CGImage and CIImage, are merely ways of obtaining what is wrapped; only one of them will work on any given UIImage, and most of the time it will be CGImage.

提交回复
热议问题