首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将`UIImage`上的`UIColor`放置在特定的(X,Y)位置?

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

Stack Overflow用户
提问于 2016-04-07 14:27:32
回答 1查看 95关注 0票数 0

我试图将UIColor放在UIImage的特定(X,Y)位置,

但是不能得到它,

在这里,我的代码如下所示:

下面的方法从UIImage的特定(X,Y)位置返回一个UIColor

代码语言:javascript
复制
- (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();

请注意,我想要的功能,如从特定位置获取UIColor,并将该UIColor放在另一个空白图像在相同的位置。

用上面的代码我不能得到这个,请让我知道我在哪里做错了。

希望,为了得到肯定的答复,

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-04-07 19:14:46

通过使用uiView,你可以像that.Actually一样得到x,y,width和height

代码语言:javascript
复制
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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36468265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档