从 NSData 创建 UiImage

发布于 2024-08-04 18:49:19 字数 1258 浏览 5 评论 0原文

下面是我(从这个网站)复制的代码,并且只做了一些修改,因为原始代码无法编译。我想操纵字节数组进行边缘检测并最终对颜色进行简单的更改,但首先我想让基本代码正常工作。目前,系统已编译并运行。它在屏幕上显示了一只画得很糟糕的大象。当我触摸图像时,它就消失了。逐步执行将 imageWithData 的结果显示为 0x0。我已经用 png 和 bmp 尝试过,结果相同

有任何线索说明我做错了什么吗?!

ImageViewDrawable 定义为:

@interface ImageViewDrawable : UIImageView

// I am using the following code to initialize this ImageView
ImageViewDrawable * uiv = [[ImageViewDrawable alloc] initWithImage:[UIImage imageNamed:@"ele.png"] ];

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // get and work on pixel data
    NSData* pixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(self.image.CGImage));
    char* bytes =[pixelData bytes];

    // Take away the red pixel, assuming 32-bit RGBA
    for(int i = 0; i < [pixelData length]; i += 4) {
        bytes[i] = bytes[i]; // red
        bytes[i+1] = bytes[i+1]; // green
        bytes[i+2] = bytes[i+2]; // blue
        bytes[i+3] = bytes[i+3]; // alpha
    }

    // convert pixel back to uiiimage
    NSData* newPixelData = [NSData dataWithBytes:bytes length:[pixelData length]];
    char * bytes2 =[pixelData bytes];   
    UIImage * newImage = [UIImage imageWithData:newPixelData] ; 
    [self setImage: newImage ]; 
}

Below is code I copied (from this site) and modified only slightly since the original code would not compile. I want to manipulate the byte array for edge detection and eventually simple changes to colors, but first I wanted to get the basic code working. Currently, the system compiles and runs. It displays a badly drawn elephant on screen. When I touch the image, it disappears. Stepping through shows the result of imageWithData as 0x0. I have tried this with both a png and a bmp and same result

Any clues to what I am doing wrong ?!

ImageViewDrawable is defined as:

@interface ImageViewDrawable : UIImageView

// I am using the following code to initialize this ImageView
ImageViewDrawable * uiv = [[ImageViewDrawable alloc] initWithImage:[UIImage imageNamed:@"ele.png"] ];

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // get and work on pixel data
    NSData* pixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(self.image.CGImage));
    char* bytes =[pixelData bytes];

    // Take away the red pixel, assuming 32-bit RGBA
    for(int i = 0; i < [pixelData length]; i += 4) {
        bytes[i] = bytes[i]; // red
        bytes[i+1] = bytes[i+1]; // green
        bytes[i+2] = bytes[i+2]; // blue
        bytes[i+3] = bytes[i+3]; // alpha
    }

    // convert pixel back to uiiimage
    NSData* newPixelData = [NSData dataWithBytes:bytes length:[pixelData length]];
    char * bytes2 =[pixelData bytes];   
    UIImage * newImage = [UIImage imageWithData:newPixelData] ; 
    [self setImage: newImage ]; 
}

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

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

发布评论

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

评论(1

谈下烟灰 2024-08-11 18:49:19

imageWithData:不接受一些任意数据并将其解释为 RGBA 未压缩纹理。它获取包含可识别图像格式(如 JPG、PNG 或 TIFF)字节的数据,解析这些数据并适当解压缩图像。

为了执行您想要的操作,您需要创建一个适当配置的 CGContext(行字节、像素格式等),或者使用您已经分配的内存作为其后备存储,或者通过询问其存储然后复制你的字节到它里面。一旦你这样做了,你就可以使用所有 CGContext 相关的功能,包括从该上下文创建 UIImage 的能力。

imageWithData: doesn't take some arbitrary data and interpret it as an RGBA uncompressed texture. It takes a data that contains bytes of a recognized image format (like JPG, PNG, or TIFF), parses thoses and decompresses the images appropriately.

In order to do what you want you need to create a CGContext which is appropriately configured (row bytes, pixel format, etc), either using the memory you already have allocated as its backing storage, or by asking it for its storage and then copying your bytes into it. Once you do that you can use all of the CGContext related functionality, including the ability to create a UIImage from that context.

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