我可以编辑 UIImage 属性 CGImage 的像素吗 [英] Can I edit the pixels of the UIImage's property CGImage

查看:34
本文介绍了我可以编辑 UIImage 属性 CGImage 的像素吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIImage 有一个只读属性 CGImage.我必须将其像素读取到内存块中并对其进行编辑,然后制作一个新的 UIImage 来替换旧的.我想知道是否有办法绕过只读属性并直接编辑这些像素.

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 there is a way bypass the read-only property and edit those pixels directly.

谢谢.

谢谢大家.我找到了一种方法来做到这一点.用这些方法写一个类:

Thanks all. I have found a way to do it. Write a class with those method:

-(void)preProcess:(UIImage*)srcImage {
    m_Context = ...// Created by calling CGBitmapContextCreate(...)
    ...
    CGContextDrawImage(m_Context, rect, srcImage.CGImage);
    m_Bits = (unsigned char*)CGBitmapContextGetData (mContext);
}

-(void)postProcess {
    CGContextRelease(m_Context);
    free(m_Bits);
}

-(UIImage*)doProcess:(CGPoint)pt {// just a example 
    unsigned char* ppxl = m_Bits + ...
    // do something...
    CGImageRef imRef = CGBitmapContextCreateImage(mContext);
    return [UIImage imageWithCGImage:imRef];
}

并且 preProcess 和 postProcess 只调用一次.

And preProcess and postProcess are called just once.

推荐答案

这个 可以帮到你.

完成后,您可以使用CGImageCreate创建一个CGImageRef,然后使用+[UIImage imageWithCGImage:]创建一个新的UIImage.

When you're done, you can use CGImageCreate to create a CGImageRef, then use +[UIImage imageWithCGImage:] to create a new UIImage.

这篇关于我可以编辑 UIImage 属性 CGImage 的像素吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆