Assuming that the "bounding box of the image" is simply a rectangle in the image specified in pixel coordinates.
You need an image rectangle that contains all pixels with alpha greater than threshold (this is equivalent to all pixels that are not in this rectangle have alpha below threshold ). After that, you can convert this rectangle to the screen coordinate (or whatever).
The basic algorithm is to start with a rectangle containing the entire image, then compress the rectangle horizontally, then vertically (or vertically, then horizontally).
I do not know Objective-C, so I put the code in pure C (some functions just to make the code more understandable):
typedef struct Rectangle { unsigned int x1, y1, x2, y2; } Rectangle; typedef struct Image { unsigned int height,width; unsigned int* data; } Image; unsigned char getPixelAlpha(Image* img, unsigned int x, unsigned int y) { unsigned int pixel = 0; // default = fully transparent if(x >= img->width || y >= img->height) return pixel; // Consider everything not in the image fully transparent pixel = img->data[x + y * img->width]; return (unsigned char)((pixel & 0xFF000000) >> 24); } void shrinkHorizontally(Image* img, unsigned char threshold, Rectangle* rect) { int x, y; // Shrink from left for(x = 0; x < (int)img->width; x++) { // Find the maximum alpha of the vertical line at x unsigned char lineAlphaMax = 0; for(y = 0; y < (int)img->height; y++) { unsigned char alpha = getPixelAlpha(img,x,y); if(alpha > lineAlphaMax) lineAlphaMax = alpha; } // If at least on pixel of the line if more opaque than 'threshold' // then we found the left limit of the rectangle if(lineAlphaMax >= threshold) { rect->x1 = x; break; } } // Shrink from right for(x = img->width - 1; x >= 0; x--) { // Find the maximum alpha of the vertical line at x unsigned char lineAlphaMax = 0; for(y = 0; y < (int)img->height; y++) { unsigned char alpha = getPixelAlpha(img,x,y); if(alpha > lineAlphaMax) lineAlphaMax = alpha; } // If at least on pixel of the line if more opaque than 'threshold' // then we found the right limit of the rectangle if(lineAlphaMax >= threshold) { rect->x2 = x; break; } } } // Almost the same than shrinkHorizontally. void shrinkVertically(Image* img, unsigned char threshold, Rectangle* rect) { int x, y; // Shrink from up for(y = 0; y < (int)img->height; y++) { // Find the maximum alpha of the horizontal line at y unsigned char lineAlphaMax = 0; for(x = 0; x < (int)img->width; x++) { unsigned char alpha = getPixelAlpha(img,x,y); if(alpha > lineAlphaMax) lineAlphaMax = alpha; } // If at least on pixel of the line if more opaque than 'threshold' // then we found the up limit of the rectangle if(lineAlphaMax >= threshold) { rect->y1 = x; break; } } // Shrink from bottom for(y = img->height- 1; y >= 0; y--) { // Find the maximum alpha of the horizontal line at y unsigned char lineAlphaMax = 0; for(x = 0; x < (int)img->width; x++) { unsigned char alpha = getPixelAlpha(img,x,y); if(alpha > lineAlphaMax) lineAlphaMax = alpha; } // If at least on pixel of the line if more opaque than 'threshold' // then we found the bottom limit of the rectangle if(lineAlphaMax >= threshold) { rect->y2 = x; break; } } } // Find the 'real' bounding box Rectangle findRealBoundingBox(Image* img, unsigned char threshold) { Rectangle r = { 0, 0, img->width, img->height }; shrinkHorizontally(img,threshold,&r); shrinkVertically(img,threshold,&r); return r; }
Now that you have the coordinates of the bounding box in a pixel on your image, you should be able to convert it to the coordinates of the device.