本文介绍了我可以编辑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的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:13