2016-04-07 40 views
0

我想放置在特定的UIColor(X,Y)的UIImage位置,如何將'UIColor'放在'UIImage`的特定(X,Y)位置?

但沒有能夠得到它,

在這裏,我的代碼看起來像下面,

以下方法還給我從特別是UIColor(X,Y)的UIImage

- (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˚F從特定的位置,並將UIColor放在另一張空白圖像上的相同位置。

有了上面的代碼,我無法得到這個,請讓我知道我犯了什麼錯誤。

希望得到有利回覆,

在此先感謝。

回答

0

使用的UIView你可以這樣做一個像that.Actually你得到的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]; 
相關問題