在IOS中,载入一张图片到CGImage后,如果想查看图片的信息,这里有一段比较好的代码可以帮助你,其他的信息要显示可以参考苹果的API自己设置!
-(void)imageDump:(NSString*)file
{
UIImage* image = [UIImage imageNamed:file];
CGImageRef cgimage = image.CGImage;
size_t width = CGImageGetWidth(cgimage);
size_t height = CGImageGetHeight(cgimage);
size_t bpr = CGImageGetBytesPerRow(cgimage);
size_t bpp = CGImageGetBitsPerPixel(cgimage);
size_t bpc = CGImageGetBitsPerComponent(cgimage);
size_t bytes_per_pixel = bpp / bpc;
CGBitmapInfo info = CGImageGetBitmapInfo(cgimage);
NSLog(
@"\n"
"===== %@ =====\n"
"CGImageGetHeight: %d\n"
"CGImageGetWidth: %d\n"
"CGImageGetColorSpace: %@\n"
"CGImageGetBitsPerPixel: %d\n"
"CGImageGetBitsPerComponent: %d\n"
"CGImageGetBytesPerRow: %d\n"
"CGImageGetBitmapInfo: 0x%.8X\n"
" kCGBitmapAlphaInfoMask = %s\n"
" kCGBitmapFloatComponents = %s\n"
" kCGBitmapByteOrderMask = %s\n"
" kCGBitmapByteOrderDefault = %s\n"
" kCGBitmapByteOrder16Little = %s\n"
" kCGBitmapByteOrder32Little = %s\n"
" kCGBitmapByteOrder16Big = %s\n"
" kCGBitmapByteOrder32Big = %s\n",
file,
(int)width,
(int)height,
CGImageGetColorSpace(cgimage),
(int)bpp,
(int)bpc,
(int)bpr,
(unsigned)info,
(info & kCGBitmapAlphaInfoMask) ? "YES" : "NO",
(info & kCGBitmapFloatComponents) ? "YES" : "NO",
(info & kCGBitmapByteOrderMask) ? "YES" : "NO",
(info & kCGBitmapByteOrderDefault) ? "YES" : "NO",
(info & kCGBitmapByteOrder16Little) ? "YES" : "NO",
(info & kCGBitmapByteOrder32Little) ? "YES" : "NO",
(info & kCGBitmapByteOrder16Big) ? "YES" : "NO",
(info & kCGBitmapByteOrder32Big) ? "YES" : "NO"
);
CGDataProviderRef provider = CGImageGetDataProvider(cgimage);
NSData* data = (id)CGDataProviderCopyData(provider);
[data autorelease];
const uint8_t* bytes = [data bytes];
printf("Pixel Data:\n");
for(size_t row = 0; row < height; row++)
{
for(size_t col = 0; col < width; col++)
{
const uint8_t* pixel =
&bytes[row * bpr + col * bytes_per_pixel];
printf("(");
for(size_t x = 0; x < bytes_per_pixel; x++)
{
printf("%.2X", pixel[x]);
if( x < bytes_per_pixel - 1 )
printf(",");
}
printf(")");
if( col < width - 1 )
printf(", ");
}
printf("\n");
}
}
效果如下:::
===== a.png =====
CGImageGetHeight: 5
CGImageGetWidth: 3
CGImageGetColorSpace: (kCGColorSpaceDeviceRGB)
CGImageGetBitsPerPixel: 32
CGImageGetBitsPerComponent: 8
CGImageGetBytesPerRow: 20
CGImageGetBitmapInfo: 0x00000003
kCGBitmapAlphaInfoMask = YES
kCGBitmapFloatComponents = NO
kCGBitmapByteOrderMask = NO
kCGBitmapByteOrderDefault = NO
kCGBitmapByteOrder16Little = NO
kCGBitmapByteOrder32Little = NO
kCGBitmapByteOrder16Big = NO
kCGBitmapByteOrder32Big = NO
Pixel Data:
(00,00,00,FF), (FF,FF,FF,FF), (FF,00,00,FF), (00,FF,00,FF), (00,00,FF,FF)
(00,00,00,FF), (FF,FF,FF,FF), (FF,00,00,FF), (00,FF,00,FF), (00,00,FF,FF)
(FF,FF,FF,00), (FF,FF,FF,00), (FF,FF,FF,00), (FF,FF,FF,00), (FF,FF,FF,00)
===== b.png =====
CGImageGetHeight: 5
CGImageGetWidth: 3
CGImageGetColorSpace: (kCGColorSpaceDeviceRGB)
CGImageGetBitsPerPixel: 24
CGImageGetBitsPerComponent: 8
CGImageGetBytesPerRow: 15
CGImageGetBitmapInfo: 0x00000000
kCGBitmapAlphaInfoMask = NO
kCGBitmapFloatComponents = NO
kCGBitmapByteOrderMask = NO
kCGBitmapByteOrderDefault = NO
kCGBitmapByteOrder16Little = NO
kCGBitmapByteOrder32Little = NO
kCGBitmapByteOrder16Big = NO
kCGBitmapByteOrder32Big = NO
Pixel Data:
(00,00,00), (FF,FF,FF), (FF,00,00), (00,FF,00), (00,00,FF)
(00,00,00), (FF,FF,FF), (FF,00,00), (00,FF,00), (00,00,FF)
(00,00,00), (FF,FF,FF), (FF,00,00), (00,FF,00), (00,00,FF)