我有两个图像(img1 和 img2)。第一个是固定的,第二个是可拖动的。img1 具有黑色轮廓,其余像素为白色。
当我移动 img2 时,它可以与第一个相交。在这里我应该改变非相交部分的像素。所以首先,我使用一种方法来获取特定点的像素。然后,我创建了两种方法来获取两个图像帧的点坐标并将它们保存到两个表中。这两种方法的目的是在拖动img2时比较两个图像的点坐标。我的意思是当我移动 img2 时,我会选择一个位置,它是一个点,并将它与 img1 包含的所有点进行比较。如果该点不包含在 img1 中,我会更改其像素颜色。之后,我比较了两张表,我得到了不常见的点并改变了它的颜色像素。我真的很挣扎。这是我的代码片段:
-(NSMutableArray *) getPixelColorAtLocation:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.view.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:[NSString stringWithFormat:@"%d",pixel[0]]];
[array addObject:[NSString stringWithFormat:@"%d",pixel[1]]];
[array addObject:[NSString stringWithFormat:@"%d",pixel[2]]];
[array addObject:[NSString stringWithFormat:@"%d",pixel[3]]];
return array;
}
-(NSMutableArray *)getimg1Coordinate:(CGPoint)point{
NSMutableArray *array = [[NSMutableArray alloc]init];
NSMutableArray *coordinates = [[NSMutableArray alloc]init];
int c,b;
for ( c=v.frame.origin.x;c <v.frame.origin.x+v.frame.size.width;c++)
{
for(b = v.frame.origin.y ;b<v.frame.origin.y+v.frame.size.height;b++)
{
array =[self getPixelColorAtLocation:point];
int red = [ [array objectAtIndex:0] intValue];
int green = [ [array objectAtIndex:1] intValue];
int blue = [ [array objectAtIndex:2] intValue];
int alpha = [ [array objectAtIndex:3] intValue];
if( red ==255)
{
if( green == 255)
{
if( blue ==255)
{
if (alpha == 255)
{
[coordinates addObject:[NSString stringWithFormat:@"%f",point.x]];
[coordinates addObject:[NSString stringWithFormat:@"%f",point.y]];
}
}
}
}
}
}
return coordinates;
}
-(NSMutableArray *)getImgCoordinate :(CGPoint)point{
NSMutableArray *coordinates = [[NSMutableArray alloc]init];
int c,b;
for ( c=imgView.frame.origin.x;c <imgView.frame.origin.x+imgView.frame.size.width;c++)
{
for(b = imgView.frame.origin.y ;b<imgView.frame.origin.y+imgView.frame.size.height;b++)
{
[coordinates addObject:[NSString stringWithFormat:@"%f",point.x]];
[coordinates addObject:[NSString stringWithFormat:@"%f",point.y]];
}
}
return coordinates;
}
我创建了上面的两种方法来存储所有点坐标以获得非公共值。
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:[self view]];
// move the image view
[imgView setCenter:location];
NSMutableArray *coordImg1 = [[NSMutableArray alloc]init];
NSMutableArray *coordImg2 = [[NSMutableArray alloc]init];
coordImg1 = [self getImg1Coordinate:location];
coordImg2 = [ self getImg2Coordinate:location];
for (id obj in coordImg) {//each obj in arr2
if ([coordBubble containsObject:obj])
{//if arr1 has the same obj(which is from arr2)
}
else {
[self changePixels];
}
}
在这里我得到了问题,我怎样才能只得到我应该改变的像素。我不确定我在做什么是最终得到我需要的最好的方法。
- (void)changePixels {
CGImageRef imageRef = [img CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace,kCGImageAlphaPremultipliedFirst| kCGBitmapByteOrder32Big);
float red = 0.0,green = 0.0,blue = 0.0,alpha = 0.0;
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
for(int xx = 0; xx<width; xx++) {
for(int yy = 0; yy<height; yy++) {
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
red = rawData[byteIndex];
green = rawData[byteIndex + 1];
blue = rawData[byteIndex + 2];
alpha = rawData[byteIndex + 3];
if( alpha ==255.000000)
{
rawData[byteIndex] = 20;
rawData[byteIndex+1] = green;
rawData[byteIndex+2] = 140;
rawData[byteIndex+3] = 60;
}
}
}
CGContextRef contextref = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
UIImage *result = [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextref)];
CGContextDrawImage(contextref, CGRectMake(0, 0, width, height), imageRef);
UIImageView *imgV= [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
[imgV setImage:result];
[self.view addSubview:imgV];
}
我现在正在努力优化我的代码,但目前我只能获取每个图像的坐标,但我还不能更改非公共部分的颜色像素。你知道我应该如何进行吗?