2

現在、この手法を使用して、UIimage のピクセルの色を取得しています。(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;

}

ここに示すように;

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

非常にうまく機能しますが、UIImageView よりも大きな画像を操作すると失敗します。画像を追加して、ビューに合わせてスケーリング モードを変更してみました。スケーリングされた画像でピクセルの色をサンプリングできるように、コードをどのように変更しますか。

4

3 に答える 3

2

これを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)
}
于 2016-06-22T10:30:12.277 に答える
0

ここにポインタがあります:

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

実際のところ: 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;

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

これは実際にはうまくいきませんでした...多くのテストを行ったわけではなく、私は世界で最も優れたプログラマーではありません(まだ)...

私の解決策は、そのUIImageView中の画像の各ピクセルが画面上の標準の CGPoint と同じサイズになるように をスケーリングすることでした。次に、通常のように色を取り ( を使用getPixelColorAtLocation:(CGPoint)point) 、次に画像を元のサイズにスケーリングしました。私は欲しかった。

お役に立てれば!

于 2012-08-30T09:40:33.673 に答える
-1

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;
}
于 2012-08-30T10:07:11.413 に答える