来自 http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/
下载:https://github.com/PaulSolt/UIImage-Conversion
ImageHelper.h
@interface ImageHelper : NSObject {
+ (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *)image;
+ (CGContextRef) newBitmapRGBA8ContextFromImage:(CGImageRef)image;
+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *)buffer width
;
ImageHelper.m
@implementation ImageHelper
+ (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *) image {
;
// Create a bitmap context to draw the uiimage into
];
{
;
}
);
);
);
// Draw image into the context to get the raw image data
);
// Get a pointer to the data
);
// Copy the data and release the memory (return memory allocated with new)
);
;
;
{
);
// Copy the data
{
];
}
}
);
{
);
}
);
+ (CGContextRef) newBitmapRGBA8ContextFromImage:(CGImageRef) image {
;
;
;
;
;
;
);
);
;
;
();
{
);
;
}
// Allocate memory for image data
);
{
);
);
;
}
//Create bitmap context
// RGBA
{
);
);
}
);
+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer
width
{
;
);
;
;
;
();
{
);
);
;
}
;
// data provider
// decode
// should interpolate
);
);
{
);
);
);
;
}
{
);
);
}
;
{
);
);
// Support both iPad 3.2 and iPhone 4 Retina displays with the correct scale
{
];
];
{
];
}
}
);
);
);
{
);
;
NSString *path = (NSString*)[[NSBundle mainBundle] pathForResource:@"Icon4" ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; int width = image.size.width; int height = image.size.height; // Create a bitmap unsigned char *bitmap = [ImageHelper convertUIImageToBitmapRGBA8:image]; // Create a UIImage using the bitmap UIImage *imageCopy = [ImageHelper convertBitmapRGBA8ToUIImage:bitmap withWidth:width withHeight:height]; // Display the image copy on the GUI UIImageView *imageView = [[UIImageView alloc] initWithImage:imageCopy]; // Cleanup free(bitmap);