来自 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);

相关文章:

  • 2021-09-18
  • 2022-12-23
  • 2021-09-28
  • 2021-09-04
  • 2022-12-23
  • 2021-04-17
  • 2021-11-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案