iPhone - 用另一个像素块替换图像中的一个像素块

发布于 2024-10-10 09:23:08 字数 231 浏览 5 评论 0原文

我想知道是否有人可以为我指出关于哪些框架/方法的正确方向 当用另一个像素块替换图像中的一个像素块时使用。

例如,假设我有一个全红色的简单图像,我认为我的像素块大小为 5x5 像素。现在,每 3 个块我想用蓝色块(5x5 像素)替换第三个块,所以 这会产生一个红色图像,每 3 个红色块就会出现一个蓝色块。

我找到了有关像素替换的信息,但正在尝试找出如何确定和处理像素块。任何建议将不胜感激..任何代码都将是一场梦魇。

I would like to know if anyone can point me in the right direction regarding what frameworks/methods
to use when replacing a block of pixels in an image with another block of pixels.

For example lets say I have a simple image colored fully red and I consider my pixel block size as 5x5
pixels. Now every 3 blocks I want to replace the 3rd block with a blue colored block (5x5 pixels) so
that it would result in a red image with blue blocks appearing every 3 red blocks.

I've found information regarding pixel replacement but am trying to figure out how to determine and process pixel blocks. Any advice would be appreciated.. and any code would be a wet dream.

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

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

发布评论

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

评论(1

冷情 2024-10-17 09:23:08

虽然这听起来像是一个简单的问题,但要做到这一点并不容易。您需要大量背景信息才能使一切顺利进行。它还取决于图像的来源以及您想用它做什么。

我不会为您提供一个工作示例,但这里有一些提示:

读取图像:

UIImage *img = [UIImage imageNamed:@"myimage.png"];
// make sure you added myimage.png to your project

创建您自己的“图形上下文”以进行绘制:

UIGraphicsBeginImageContext(CGSizeMake(480.0f, 320.0f)); // the size (width,height)
CGContextRef ctx = UIGraphicsGetCurrentContext();

将图像绘制到(当前)上下文上:

[img drawAtPoint:CGPointMake(0.0f, -480.0f)]; // (draws on current context)

进行其他绘制:

CGContextBeginPath(ctx); // start a path
CGContextSetRGBStrokeColor(ctx,1,0,0,1); // red,green,blue,alpha with range 0-1
CGContextMoveToPoint(ctx,50,0); // move to beginning of path at point (50,0)
CGContextAddLineToPoint(ctx,100,100); // add a line to point (100,100)
CGContextStrokePath(ctx); // do the drawing

转换位图上下文再次到图像:

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

然后,要么:

从视图控制器内向观众显示图像:

[self.view addSubview:[[[UIImageView alloc] initWithImage:resultImage] autorelease]];

或者将图像写为 jpeg:

NSData *imagedata = UIImageJPEGRepresentation(img,0.8); // 0.8 = quality
[imagedata writeToFile:filename atomically:NO];

或者 PNG:

NSData *imagedata = UIImagePNGRepresentation(img);
[imagedata writeToFile:filename atomically:NO];

对于最后的图像,您需要有一个文件名,但事实并非如此两者都是微不足道的(考虑到您所居住的手机环境)。这是另一个你会发现很多的主题,搜索如下内容:

NSArray *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filename = [[path elementAtIndex:0] stringByAppendingPathComponent:@"myimage.png"];

这是总体轮廓,我可能犯了一些错误,但这至少应该让你继续下去。

Although it may sound like a simple question, it's not quite easy to do something like that. You need a lot of background information to get everything going. It also depends on where the image comes from, and what you want to do with it.

I will not provide you with a working example, but here's some pointers:

Reading in an image:

UIImage *img = [UIImage imageNamed:@"myimage.png"];
// make sure you added myimage.png to your project

Creating your own "graphics context" to draw on:

UIGraphicsBeginImageContext(CGSizeMake(480.0f, 320.0f)); // the size (width,height)
CGContextRef ctx = UIGraphicsGetCurrentContext();

Painting your image onto your (current) context:

[img drawAtPoint:CGPointMake(0.0f, -480.0f)]; // (draws on current context)

Doing some other painting:

CGContextBeginPath(ctx); // start a path
CGContextSetRGBStrokeColor(ctx,1,0,0,1); // red,green,blue,alpha with range 0-1
CGContextMoveToPoint(ctx,50,0); // move to beginning of path at point (50,0)
CGContextAddLineToPoint(ctx,100,100); // add a line to point (100,100)
CGContextStrokePath(ctx); // do the drawing

Transforming the bitmap context to an image again:

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

And then, either:

Show the image to the audience, from within a view controller:

[self.view addSubview:[[[UIImageView alloc] initWithImage:resultImage] autorelease]];

Or write the image as jpeg:

NSData *imagedata = UIImageJPEGRepresentation(img,0.8); // 0.8 = quality
[imagedata writeToFile:filename atomically:NO];

Or as PNG:

NSData *imagedata = UIImagePNGRepresentation(img);
[imagedata writeToFile:filename atomically:NO];

For those last ones you would need to have a filename, which is not so trivial either (given the phone environment you live in). That's another topic on which you'll find a lot, search for things like:

NSArray *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filename = [[path elementAtIndex:0] stringByAppendingPathComponent:@"myimage.png"];

This is the general outline, I might have made some errors but this should get you going at least.

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