简体   繁体   English

如何在特定的(X,Y)位置将UIColor放在UIImage上?

[英]How to place the `UIColor` on `UIImage` at particular (X,Y) position?

I am trying to place UIColor on particular (X,Y) position of UIImage , 我正在尝试将UIColor放置在UIImage特定(X,Y)位置上,

But not able to get it, 但是没办法

Here, My code looks like below, 在这里,我的代码如下所示

Below method return me a UIColor from particular (X,Y) position of UIImage 下面的方法从UIImage特定(X,Y)位置返回一个UIColor

- (UIColor *)isWallPixel:(UIImage *)image xCoordinate:(int)x yCoordinate:(int)y {

   CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
   const UInt8* data = CFDataGetBytePtr(pixelData);

   int pixelInfo = ((image.size.width  * y) + x ) * 4; // The image is png

   UInt8 red = data[pixelInfo];         // If you need this info, enable it
   UInt8 green = data[(pixelInfo + 1)]; // If you need this info, enable it
   UInt8 blue = data[pixelInfo + 2];    // If you need this info, enable it
   UInt8 alpha = data[pixelInfo + 3];     // I need only this info for my maze game
   CFRelease(pixelData);

   UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f]; // The

  return color;
}


//Original Image, I get image pixels from this image.
UIImage* image = [UIImage imageNamed:@"plus.png"];

//Need convert Image
UIImage *imagedata = [[UIImage alloc]init];

//size of the original image
int Width = image.size.width;
int height = image.size.height;

//need to get this size of another blank image
CGRect rect = CGRectMake(0.0f, 0.0f, Width, height);

UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

for (int i = 0; i <Width; i++) {
    for (int j = 0; j < height; j++) {

       //Here I got the Image pixel color from below highlighted method
        UIColor *colordatra = [self isWallPixel:image xCoordinate:i yCoordinate:j];
        CGContextSetFillColorWithColor(context, [colordatra CGColor]);

        rect.origin.x = i;
        rect.origin.y = j;
        CGContextDrawImage(context, rect, imagedata.CGImage);
        imagedata = UIGraphicsGetImageFromCurrentImageContext();
    }
}

UIGraphicsEndImageContext();

Please note that I want to functionality like get the UIColor from particular position and placed that UIColor on another blank image at same position. 请注意,我想使用某些功能,例如从特定位置获取UIColor并将该UIColor放置在同一位置的另一个空白图像上。

With above code I am not able to get this, Please let me know where I have done the mistake. 使用上面的代码,我无法获取此信息,请让我知道我在哪里做错了。

Hope, for favourable reply, 希望得到满意的答复,

Thanks in Advance. 提前致谢。

By using uiView you can do this one like that.Actually you are getting x,y,width and height 通过使用uiView,您可以像这样进行操作。实际上,您得到的x,y,宽度和高度

uiview *view = [uiview alloc]initwithframe:CGRectMake(x,y,width,height);
view.backgroundcolor =[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f];
[self.YourImageView addsubview:view];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM