簡體   English   中英

如何獲取自定義uiimage每個像素的坐標?

[英]How to get the coordinates of each pixel of custom uiimage?

嗨,大家好

我需要編寫簡單的益智游戲,其主要條件是,當“發布”了一塊拼圖時,它就很接近目的地了。

因此,我嘗試獲取圖像每個像素的坐標數組,為此,我想將像素顏色與背景顏色進行比較,如果它們不相等,則為圖像像素的坐標。 但是..我不會怎么做。

我試過了:

- (BOOL)isImagePixel:(UIImage *)image withX:(int)x andY:(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];         
    UInt8 green = data[(pixelInfo + 1)]; 
    UInt8 blue = data[pixelInfo + 2];    
    UInt8 alpha = data[pixelInfo + 3];   
    CFRelease(pixelData);

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

        NSLog(@"color is %@",[UIColor whiteColor]);
    if ([color isEqual:self.view.backgroundColor]){
        NSLog(@"x = %d, y = %d",x,y);
        return YES;
    }
    else return NO;

}

怎么了
也許有人可以建議我其他解決方案?

謝謝。

這似乎是一個非常麻煩的解決方案。 我的建議是,對於每一塊,您都維護一個表,說它是拼圖中的左上角坐標,當用戶抬起手指時,您將計算出從當前位置到指定位置的絕對距離。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM