如何从 UIImage NSLog 像素 RGB?

发布于 2024-08-03 12:26:46 字数 914 浏览 1 评论 0原文

我只想:

1) 复制像素数据。
2) 迭代并修改每个像素(仅向我展示如何将 ARGB 值 NSLog 为 255)
3) 从新的像素数据创建 UIImage

如果有人能告诉我如何将像素的 RGBA 值 NSLog 为 255,我可以弄清楚具体的细节。我该如何修改下面的代码可以做到这一点?请具体说明!

-(UIImage*)modifyPixels:(UIImage*)originalImage
{

NSData* pixelData = (NSData*)CGDataProviderCopyData(CGImageGetDataProvider(originalImage.CGImage));
uint myLength = [pixelData length];

    for(int i = 0; i < myLength; i += 4) {


        //CHANGE PIXELS HERE
        /*
        Sidenote: Just show me how to NSLog them
        */
        //Example:
        //NSLog(@"Alpha 255-Value is: %u", data[i]);
        //NSLog(@"Red 255-Value is: %u", data[i+1]);
        //NSLog(@"Green 255-Value is: %u", data[i+2]);
        //NSLog(@"Blue 255-Value is: %u", data[i+3]);
    }

    //CREATE NEW UIIMAGE (newImage) HERE 

    return newImage;    
}

I just want to:

1) Copy the pixel data.
2) Iterate and Modify each pixel (just show me how to NSLog the ARGB values as 255)
3) Create a UIImage from the new pixel data

I can figure out the the gory details if someone can just tell me how to NSLog the RGBA values of a pixel as 255. How do I modify the following code to do this? Be Specific Please!

-(UIImage*)modifyPixels:(UIImage*)originalImage
{

NSData* pixelData = (NSData*)CGDataProviderCopyData(CGImageGetDataProvider(originalImage.CGImage));
uint myLength = [pixelData length];

    for(int i = 0; i < myLength; i += 4) {


        //CHANGE PIXELS HERE
        /*
        Sidenote: Just show me how to NSLog them
        */
        //Example:
        //NSLog(@"Alpha 255-Value is: %u", data[i]);
        //NSLog(@"Red 255-Value is: %u", data[i+1]);
        //NSLog(@"Green 255-Value is: %u", data[i+2]);
        //NSLog(@"Blue 255-Value is: %u", data[i+3]);
    }

    //CREATE NEW UIIMAGE (newImage) HERE 

    return newImage;    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-08-10 12:26:46

这个方向对你有用吗?我会得到这样的像素数据:

UInt32 *pixels = CGBitmapContextGetData( ctx );

#define getRed(p) ((p) & 0x000000FF)
#define getGreen(p) ((p) & 0x0000FF00) >> 8
#define getBlue(p) ((p) & 0x00FF0000) >> 16
// display RGB values from the 11th pixel
NSLog(@"Red: %d, Green: %d, Blue: %d", getRed(pixels[10]), getGreen(pixels[10]), getBlue(pixels[10]));

Did this direction work for you? I'd get pixel data like this:

UInt32 *pixels = CGBitmapContextGetData( ctx );

#define getRed(p) ((p) & 0x000000FF)
#define getGreen(p) ((p) & 0x0000FF00) >> 8
#define getBlue(p) ((p) & 0x00FF0000) >> 16
// display RGB values from the 11th pixel
NSLog(@"Red: %d, Green: %d, Blue: %d", getRed(pixels[10]), getGreen(pixels[10]), getBlue(pixels[10]));
西瓜 2024-08-10 12:26:46

如果您想实际查看图像,可以使用 Florent Pillet 的 NSLogger:
https://github.com/fpillet/NSLogger

这个想法是在桌面上启动 NSLogger 客户端,然后在您的应用程序中将其放在顶部:

#import "LoggerClient.h"

在您的 modifyPixels 方法中,您可以执行以下操作:

LogImageData(@"RexOnRoids",        // Any identifier to go along with the log
             0,                    // Log level
             newImage.size.width,  // Image width
             newImage.size.height, // Image height
             UIImagePNGRepresentation(newImage)); // Image as PNG

在桌面上启动客户端,然后在 iPhone 上运行该应用程序,然后您将会看到真实的图像出现在客户端中。对于调试图像问题非常方便,例如翻转、旋转、颜色、alpha 等。

If you'd like to actually see the image, you can use Florent Pillet's NSLogger:
https://github.com/fpillet/NSLogger

The idea is you start the NSLogger client on your desktop, and then in your app you put this up towards the top:

#import "LoggerClient.h"

And in your modifyPixels method you can do something like this:

LogImageData(@"RexOnRoids",        // Any identifier to go along with the log
             0,                    // Log level
             newImage.size.width,  // Image width
             newImage.size.height, // Image height
             UIImagePNGRepresentation(newImage)); // Image as PNG

Start the client on your desktop, and then run the app on your iphone, and you'll see real images appear in the client. VERY handy for debugging image problems such as flipping, rotating, colors, alpha, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文