获取 UIImageView CGPoints
Get UIImageView CGPoints
我想在我的 UIImageVeiw 中获取所有点,这样我就可以更改 "some" 点颜色而不需要 UITouch ..这可能吗?
我在想的是:
- 获取uiimageview中的所有点
- 获取每个点的颜色。
- 如果之前的颜色=某种特定的颜色,那么改变颜色。
我在谷歌上搜索了很多,但我发现所有教程都像这样依赖 UITouch http://www.markj.net/iphone-uiimage-pixel-color/
我现在的主要目标是如何获得所有积分?!
感谢任何帮助
我找到了解决方案.. 希望有一天它能对任何人有所帮助。
此方法 returns 某些图像中的像素数组。
-(NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
for (int i = 0 ; i < count ; ++i)
{
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += bytesPerPixel;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
free(rawData);
return result;}
你可以这样称呼它:
NSArray*arrayOfPixels= [self getRGBAsFromImage:_patternFirstImage.image atX:_patternFirstImage.frame.origin.x andY:_patternFirstImage.frame.origin.y count:_patternFirstImage.frame.size.width*_patternFirstImage.frame.size.height];
NSLog(@"arrayOfPixels = %zd",[arrayOfPixels count]);
您可以循环遍历所有像素以获取其颜色,如下所示:
for(int i=0;i<[arrayOfPixels count];i++){
NSLog(@"objects = ---%@----",[arrayOfPixels objectAtIndex:i]);
if([self color:[arrayOfPixels objectAtIndex:i] isEqualToColor:UIColorFromRGB(0xE0E0E0) withTolerance:0.2]){
NSLog(@"index = %zd",i);
}
}
非常感谢这里的回答:
我想在我的 UIImageVeiw 中获取所有点,这样我就可以更改 "some" 点颜色而不需要 UITouch ..这可能吗?
我在想的是:
- 获取uiimageview中的所有点
- 获取每个点的颜色。
- 如果之前的颜色=某种特定的颜色,那么改变颜色。
我在谷歌上搜索了很多,但我发现所有教程都像这样依赖 UITouch http://www.markj.net/iphone-uiimage-pixel-color/
我现在的主要目标是如何获得所有积分?!
感谢任何帮助
我找到了解决方案.. 希望有一天它能对任何人有所帮助。 此方法 returns 某些图像中的像素数组。
-(NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
for (int i = 0 ; i < count ; ++i)
{
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += bytesPerPixel;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
free(rawData);
return result;}
你可以这样称呼它:
NSArray*arrayOfPixels= [self getRGBAsFromImage:_patternFirstImage.image atX:_patternFirstImage.frame.origin.x andY:_patternFirstImage.frame.origin.y count:_patternFirstImage.frame.size.width*_patternFirstImage.frame.size.height];
NSLog(@"arrayOfPixels = %zd",[arrayOfPixels count]);
您可以循环遍历所有像素以获取其颜色,如下所示:
for(int i=0;i<[arrayOfPixels count];i++){
NSLog(@"objects = ---%@----",[arrayOfPixels objectAtIndex:i]);
if([self color:[arrayOfPixels objectAtIndex:i] isEqualToColor:UIColorFromRGB(0xE0E0E0) withTolerance:0.2]){
NSLog(@"index = %zd",i);
}
}
非常感谢这里的回答: