简体   繁体   English

如何从UIimageView中缩放的UIimage获取位置的像素颜色

[英]How to get pixel color at location from UIimage scaled within a UIimageView

I'm currently using this technique to get the color of a pixel in a UIimage. 我目前正在使用这种技术来获取UIimage中像素的颜色。 (on Ios) (在Ios上)

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
UIColor* color = nil;
CGImageRef inImage = self.image.CGImage;
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { return nil; /* error */ }

size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}}; 

// Draw the image to the bitmap context. Once we draw, the memory 
// allocated for the context for rendering will then contain the 
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage); 

// Now we can get a pointer to the image data associated with the bitmap
// context.
unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL) {
    //offset locates the pixel in the data from x,y. 
    //4 for 4 bytes of data per pixel, w is width of one row of data.
    int offset = 4*((w*round(point.y))+round(point.x));
    int alpha =  data[offset]; 
    int red = data[offset+1]; 
    int green = data[offset+2]; 
    int blue = data[offset+3]; 
    NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
    color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
}

// When finished, release the context
CGContextRelease(cgctx); 
// Free image data memory for the context
if (data) { free(data); }
return color;

} }

As illustrated here; 如此处所示;

http://www.markj.net/iphone-uiimage-pixel-color/ http://www.markj.net/iphone-uiimage-pixel-color/

it works quite well, but when working with images larger than the UIImageView it fails. 它工作得很好,但是在处理大于UIImageView的图像时会失败。 I tried adding an image and changing the scaling mode to fit the view. 我尝试添加图像并更改缩放模式以适合视图。 How would I modify the code to so that it would still be able to sample the pixel color with a scaled image. 我如何将代码修改为,以便仍然能够使用缩放图像对像素颜色进行采样。

try this for swift3 试试这个swift3

func getPixelColor(image: UIImage, x: Int, y: Int, width: CGFloat) -> UIColor 
{

    let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

    let pixelInfo: Int = ((Int(width) * y) + x) * 4

    let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
    let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
    let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
    let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

    return UIColor(red: r, green: g, blue: b, alpha: a)
}

Here's a pointer: 这是一个指针:

0x3A28213A //sorry, I couldn't resist the joke

For real now: after going through the comments on the page at markj.net, a certain James has suggested to make the following changes: 现在来看:在浏览markj.net页面上的评论后,某位James建议进行以下更改:

size_t w = CGImageGetWidth(inImage); //Written by Mark
size_t h = CGImageGetHeight(inImage); //Written by Mark
float xscale = w / self.frame.size.width;
float yscale = h / self.frame.size.height;
point.x = point.x * xscale;
point.y = point.y * yscale;

(thanks to http://www.markj.net/iphone-uiimage-pixel-color/comment-page-1/#comment-2159 ) (感谢http://www.markj.net/iphone-uiimage-pixel-color/comment-page-1/#comment-2159

This didn't actually work for me... Not that I did much testing, and I'm not the world's greatest programmer (yet)... 这实际上对我没有用……不是我做了很多测试,而且我不是世界上最伟大的程序员(至今)……

My solution was to scale the UIImageView in such a way that each pixel of the image in it was the same size as a standard CGPoint on the screen, then I took my color like normal (using getPixelColorAtLocation:(CGPoint)point ) , then I scaled the image back to the size I wanted. 我的解决方案是以某种方式缩放UIImageView ,以使其图像中的每个像素与屏幕上的标准CGPoint大小相同,然后像正常一样使用我的颜色(使用getPixelColorAtLocation:(CGPoint)point ),然后将图像缩放回我想要的大小。

Hope this helps! 希望这可以帮助!

Use the UIImageView Layer: 使用UIImageView层:

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
    UIColor* color = nil;

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef cgctx = UIGraphicsGetCurrentContext();
    if (cgctx == NULL) { return nil; /* error */ }

    [self.layer renderInContext:cgctx];

    unsigned char* data = CGBitmapContextGetData (cgctx);
    /*
    ...
    */
    UIGraphicsEndImageContext();
    return color;
}

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

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