【发布时间】:2010-12-27 01:12:24
【问题描述】:
根据对a previous question 的回复,我在 UIImageView 上创建了一个类别,用于提取像素数据。这在模拟器中可以正常工作,但在部署到设备时却不行。我应该说不是总是——奇怪的是,如果 point.x == point.y; 它确实会获取正确的像素颜色;否则,它会为我提供该线另一侧像素的像素数据,就像镜像一样。 (因此,点击图像右下角的像素会给我左上角相应像素的像素数据,但点击左下角的像素会返回正确的像素颜色)。触摸坐标(CGPoint)正确。
我做错了什么?
这是我的代码:
@interface UIImageView (PixelColor)
- (UIColor*)getRGBPixelColorAtPoint:(CGPoint)point;
@end
@implementation UIImageView (PixelColor)
- (UIColor*)getRGBPixelColorAtPoint:(CGPoint)point
{
UIColor* color = nil;
CGImageRef cgImage = [self.image CGImage];
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
NSUInteger x = (NSUInteger)floor(point.x);
NSUInteger y = height - (NSUInteger)floor(point.y);
if ((x < width) && (y < height))
{
CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
CFDataRef bitmapData = CGDataProviderCopyData(provider);
const UInt8* data = CFDataGetBytePtr(bitmapData);
size_t offset = ((width * y) + x) * 4;
UInt8 red = data[offset];
UInt8 blue = data[offset+1];
UInt8 green = data[offset+2];
UInt8 alpha = data[offset+3];
CFRelease(bitmapData);
color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f];
}
return color;
}
【问题讨论】:
-
你能检查一下self.image.imageOrientation的值吗?您正在使用的图像可能在 UIImageOrientationLeftMirrored 中,这将是您看到的反射,但我不知道为什么它只会在设备上出现......
-
模拟器和设备上都是 UIImageOrientationUp。我不确定“左镜像”是对正在发生的事情的正确解释,因为反射(似乎)沿着对角线 y=x 发生,而 UIImageOrientationLeftMirrored 是整个图像“逆时针 90 度”的简单旋转到 SDK。
-
更多信息:如果我交换 x 和 y 的计算方式,则行为会相反——它适用于设备,但不适用于模拟器。 (虽然设备上的准确定位似乎逆时针偏移了几度,但这可能是我的手指与使用鼠标相比不精确)
标签: iphone uiimageview pixel