我有一张图像有一些透明区域,我想忽略该区域。
并以最大的可能性获得那个有盖的框架..
在这里我上传了一张图片,所以你知道我在问什么。
我已经使用了一些逻辑和函数来评估这个框架,但我仍然不能准确的框架。
我想要动态代码,所以我可以改变这种图像。
在这里,我附上了我使用过的示例代码。
-(CGRect)getImageRect:(UIImage *)image{
int x1,y1,x2,y2;
BOOL flag=NO;
CGFloat red,green,blue,alpha;
UIColor *color=nil;
// Get starting position y1
flag=NO;
for (int y=0; y<image.size.height; y++) {
for (int x=0; x<image.size.width; x++) {
// Get pixel color for
color=[self getPixelColor:image withX:x withY:y];
[color getRed:&red green:&green blue:&blue alpha:&alpha];
if (red==0 && green==0 && blue==0 && alpha==0){
continue;
}else{
y1=y;
flag=YES;
break;
}
}
if (flag) {
break;
}
}
// Get starting position x1
flag=NO;
for (int x=0; x<image.size.width; x++) {
for (int y=0; y<image.size.height; y++) {
// Get pixel color for
color=[self getPixelColor:image withX:x withY:y];
[color getRed:&red green:&green blue:&blue alpha:&alpha];
if (red==0 && green==0 && blue==0 && alpha==0) {
continue;
}else {
x1=x;
flag=YES;
break;
}
}
if (flag) {
break;
}
}
// Get last position y2
flag=NO;
for (int y=image.size.height; y>=0; y--) {
for (int x=image.size.width; x>=0; x--) {
// Get pixel color for
color=[self getPixelColor:image withX:x withY:y];
[color getRed:&red green:&green blue:&blue alpha:&alpha];
if (red==0 && green==0 && blue==0 && alpha==0) {
continue;
}
else {
y2=y;
flag=YES;
break;
}
}
if (flag) {
break;
}
}
// Get last position x2
flag=NO;
for (int x=image.size.width; x>=0; x--) {
for (int y=image.size.height; y>=0; y--) {
// Get pixel color for
color=[self getPixelColor:image withX:x withY:y];
[color getRed:&red green:&green blue:&blue alpha:&alpha];
if (red==0 && green==0 && blue==0 && alpha==0) {
continue;
}
else {
x2=x;
flag=YES;
break;
}
}
if (flag) {
break;
}
}
return CGRectMake(x1, y1, x2-x1, y2-y1);
}
- (UIColor *)getPixelColor:(UIImage *)image withX:(int)x withY:(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]; // Get red info
UInt8 green = data[(pixelInfo + 1)]; // Get red info
UInt8 blue = data[pixelInfo + 2]; // Get red info
UInt8 alpha = data[pixelInfo + 3]; // I need only this info for my maze game
CFRelease(pixelData);
UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f]; // The pixel color info
return color;
}
请发表您的有用评论并回答以缩短此内容
/------------ 已编辑
我需要被橙色覆盖的框架。我画了橙色只是为了理解我的问题。它不是必需的。
问候,