iPhone - 用另一个像素块替换图像中的像素块 [英] iPhone - Replacing a block of pixels in an image with another block of pixls

查看:142
本文介绍了iPhone - 用另一个像素块替换图像中的像素块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,假设我有一个完全红色的简单图像,我认为我的像素块大小为5x5
像素。现在每3块我想要用蓝色块(5x5像素)替换第3块,所以
将导致红色图像,每3个红色块出现蓝色块。

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.

推荐答案

虽然这可能听起来像一个简单的问题,但做这样的事情并不容易。您需要大量背景信息才能完成所有工作。它还取决于图像的来源,以及你想用它做什么。

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:

读取图片:

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

创建你自己的图形contextto draw on:

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)

做一些其他的绘画:

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();

然后,要么:

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

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

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

或者将图像写为jpeg:

Or write the image as 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];

对于那些最后的文件,你需要有一个文件名,这也不是那么简单(给定你住的电话环境)。这是你会发现很多的另一个主题,搜索如下内容:

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.

这篇关于iPhone - 用另一个像素块替换图像中的像素块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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