在IOS中从UIImagview检测黑色的最左侧点
我无法从检测在IOS中从UIImagview检测黑色的最左侧点,ios,objective-c,uiimageview,position,core-graphics,Ios,Objective C,Uiimageview,Position,Core Graphics,我无法从检测UIImageView中找到最左边的点/帧/位置。我尝试从UIImageView检测黑色像素,如下所示: -(void)Black_findcolor { UIImage *image = imgview.image;//imgview from i want to detect the most black color pixels. CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvid
UIImageView
中找到最左边的点/帧/位置。我尝试从UIImageView
检测黑色像素,如下所示:
-(void)Black_findcolor
{
UIImage *image = imgview.image;//imgview from i want to detect the most black color pixels.
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
int myWidth = CGImageGetWidth(image.CGImage);
int myHeight = CGImageGetHeight(image.CGImage);
const UInt8 *pixels = CFDataGetBytePtr(pixelData);
UInt8 blackThreshold = 10 ;
// UInt8 alphaThreshold = 100;
int bytesPerPixel_ = 4;
CGFloat Xvalue,Yvalue;
int x = 0,y=0;
for( x = 0; x < myWidth; x++)
{
for( y = 0; y < myHeight; y++)
{
int pixelStartIndex = (x + (y * myWidth)) * bytesPerPixel_;
UInt8 alphaVal = pixels[pixelStartIndex];
UInt8 redVal = pixels[pixelStartIndex + 1];
UInt8 greenVal = pixels[pixelStartIndex + 2];
UInt8 blueVal = pixels[pixelStartIndex + 3];
if(redVal < blackThreshold || blueVal < blackThreshold || greenVal < blackThreshold || alphaVal < blackThreshold)
{
NSLog(@"x =%d, y = %d", x, y);
}
}
}
}
那么,有谁能给我一些想法或建议,如何设置imgpin1最左侧黑色像素的帧或位置呢
如果需要任何其他信息,请告诉我
提前感谢。已编辑
请看一下我编辑过的答案
//You need to get resized image from your imageView in case of sizeToFit etc.
float oldWidth = yourImageView.image.size.width;
float scaleFactor = yourImageView.frame.size.width / oldWidth;
float newHeight = yourImageView.image.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[yourImageView.image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize size = newImage.size;
int width = size.width;
int height = size.height;
int xCoord = width, yCoord = height;
UInt8 blackThreshold = 10;
// raw data will be assigned into this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
// fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [yourImageView.image CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
// convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
uint32_t gray = 0.3 * rgbaPixel[1] + 0.59 * rgbaPixel[2] + 0.11 * rgbaPixel[3];
if (gray < blackThreshold)
{
if (x<xCoord)
{
xCoord = x;
yCoord = y;
NSLog(@"X:%d Y:%d",xCoord,yCoord);
}
}
}
}
// we're done with the context and pixels
CGContextRelease(context);
free(pixels);
imgPin1.center=CGPointMake(xCoord, yCoord);
//如果存在sizeToFit等问题,您需要从imageView中获取已调整大小的图像。
float oldWidth=yourImageView.image.size.width;
float scaleFactor=yourImageView.frame.size.width/oldWidth;
float newHeight=yourImageView.image.size.height*scaleFactor;
float newWidth=oldWidth*scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth,newHeight));
[yourImageView.image drawInRect:CGRectMake(0,0,newWidth,newHeight)];
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
CGSize size=newImage.size;
int width=size.width;
int height=size.height;
int xCoord=宽度,yCoord=高度;
UInt8黑阈值=10;
//原始数据将分配到此数组中
uint32_t*像素=(uint32_t*)malloc(宽度*高度*尺寸(uint32_t));
//清除像素以保留任何透明度
memset(像素,0,宽度*高度*大小(uint32_t));
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
//使用RGBA像素创建上下文
CGContextRef context=CGBitmapContextCreate(像素、宽度、高度、8、宽度*sizeof(uint32_t)、颜色空间、,
kCGBitmapByteOrder32Little | KCGimageAlphaPremultipledLast);
//填充像素数组
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),[yourImageView.image CGImage]);
对于(int y=0;y 如果(X感谢您的回答…但在我的UIImageview上没有检测到任何黑色像素…您能帮我吗?当然。让我在我的xcode中尝试一下。您使用的是什么图像?编辑了我的问题,我使用了该图像。在这一点上,我想将黑色条带作为检测。请查看我编辑的答案。按照您的要求进行测试和工作ired.非常感谢…你能告诉我什么是scater吗?其他方面,你能在这里分享整个项目吗…对我来说很好。所以你想要你的pin在卡片的开头黑色条纹是吗?是的,完全正确…最左边和最右边…是的,我正在测试一些变体。过一会儿会让你知道的
//You need to get resized image from your imageView in case of sizeToFit etc.
float oldWidth = yourImageView.image.size.width;
float scaleFactor = yourImageView.frame.size.width / oldWidth;
float newHeight = yourImageView.image.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[yourImageView.image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize size = newImage.size;
int width = size.width;
int height = size.height;
int xCoord = width, yCoord = height;
UInt8 blackThreshold = 10;
// raw data will be assigned into this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
// fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [yourImageView.image CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
// convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
uint32_t gray = 0.3 * rgbaPixel[1] + 0.59 * rgbaPixel[2] + 0.11 * rgbaPixel[3];
if (gray < blackThreshold)
{
if (x<xCoord)
{
xCoord = x;
yCoord = y;
NSLog(@"X:%d Y:%d",xCoord,yCoord);
}
}
}
}
// we're done with the context and pixels
CGContextRelease(context);
free(pixels);
imgPin1.center=CGPointMake(xCoord, yCoord);