获取UIImageview图像的框架 - iOS

时间:2012-09-04 10:17:38

标签: iphone ipad object uiimage core-graphics

我有一张图片有一些透明区域,我想忽略那个区域。

最大限度地获得覆盖的框架..

这里我上传了一张图片,让您了解我的要求。

enter image description here

我已经使用了一些逻辑和函数来评估这个帧,但我仍然无法确切的帧。

我想要动态代码,这样我就可以改变这种形象。

这里我附上了我用过的示例代码。

-(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;
} 

请发布您的有用评论和答案以简短

/ ---------- 编辑

我需要用橙色覆盖的框架。我画了橙色只是为了理解我的问题。它不是必需的。

此致

0 个答案:

没有答案