從圖像中讀取像素,從圖像中獲取CGRect
與設備寬度和高度比較。
- (CGRect)getRectFromImage:(UIImage*)image
{
// 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 = malloc(height * width * 4);
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);
int x = 0;
int y = 0;
int xPos = 0;
int yPos = 0;
int xMax = 0;
int yMax = 0;
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
unsigned char alphaByte = rawData[(y*bytesPerRow)+(x*bytesPerPixel)+3];
if (alphaByte > 0) {
if (xPos == 0) {
xPos = x;
}
if (yPos == 0) {
yPos = y;
}
if (x < xPos) {
xPos = x;
}
if (y < yPos) {
yPos = y;
}
if (x > xMax) {
xMax = x;
}
if (y > yMax) {
yMax = y;
}
}
}
}
NSLog(@"(%i,%i,%i,%i)", xPos, yPos,xMax-xPos,yMax-yPos);
free(rawData);
return CGRectMake(xPos, yPos, xMax-xPos, yMax-yPos);
}
我想從掩碼動態幀這僅僅是例子,我有很多不同的掩膜圖像 –