Can I edit the pixels of the UIImage's property CGImage

前端 未结 7 1466
野趣味
野趣味 2020-11-28 05:04

UIImage has a read-only property CGImage. I have to read its pixels to a memory block and edit them and then make a new UIImage to replace the old one. I want to know if th

7条回答
  •  一个人的身影
    2020-11-28 05:11

    The short answer is no. However, you say you have to make a copy anyhow, so why not just get an NSData object and manipulate its bytes.

    From the Apple docs on UIImage:

    Because image objects are immutable, they also do not provide direct access to their underlying image data. However, you can get an NSData object containing either a PNG or JPEG representation of the image data using the UIImagePNGRepresentation and UIImageJPEGRepresentation functions.

    To get the data as a PNG, use:

    NSData * UIImagePNGRepresentation (
       UIImage *image
    );
    

    for JPEG, use:

    NSData * UIImageJPEGRepresentation (
       UIImage *image,
       CGFloat compressionQuality
    );
    

提交回复
热议问题