@FrankSchlegel
thank you for ur answer. I just setting up CGContext like this. even I just put Device colorspace, It's same wrong result image. here's the code.
(and I try to edit my post but I can only edit my reply ;;)
(UIImage *)resizeImage:(UIImage *)originalImage size:(CGSize)size
{
@autoreleasepool {
CGContextRef context=nil;
CGColorSpaceRef colorSpace=nil;
CGSize ceiledSize = CGSizeMake(ceil(size.width), ceil(size.height));
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGFloat pixelsWidth = ceiledSize.width;
CGFloat pixelsHeight = ceiledSize.height;
CGFloat bitmapBytesPerRow = (pixelsWidth * 4);// 1
CGFloat bitmapByteCount = (bitmapBytesPerRow * pixelsHeight);
uint8_t *bitmapData = calloc( bitmapByteCount, sizeof(uint8_t));
context = CGBitmapContextCreate(bitmapData, ceiledSize.width, ceiledSize.height, 8, bitmapBytesPerRow,
colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, ceiledSize.width, ceiledSize.height), originalImage.CGImage);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage *resultImage = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(bitmapData);
return resultImage;
}
}
Post
Replies
Boosts
Views
Activity
I try to use vImage functions too. but the result image changed color.
(UIImage *)resizeImage:(UIImage *)originalImage size:(CGSize)size
{
// CGColorSpaceRef colorspace = CGImageGetColorSpace(originalImage.CGImage);
// CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
vImage_CGImageFormat format;
format.bitsPerComponent = 8;
format.bitsPerPixel = 32;
format.bitmapInfo = (CGBitmapInfo)(kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault); //ARGB
// format.bitmapInfo = (CGBitmapInfo)(kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); //RGBA
format.decode = nil;
format.colorSpace = colorspace;
format.renderingIntent = kCGRenderingIntentDefault;
CGSize ceiledSize = CGSizeMake(ceil(size.width), ceil(size.height));
vImage_Error error = kvImageNoError;
// Create source buffer
vImage_Buffer sourceBuffer;
error = vImageBuffer_InitWithCGImage(&sourceBuffer, &format, nil, originalImage.CGImage, kvImageNoFlags);
if(error != kvImageNoError) {
NSLog(@"ERROR: vImage SOURCE IMAGE INIT");
return NULL;
}
// Create target Buffer
vImage_Buffer targetBuffer;
error = vImageBuffer_Init(&targetBuffer, ceiledSize.height, ceiledSize.width, format.bitsPerPixel, kvImageNoFlags);
if(error != kvImageNoError) {
NSLog(@"ERROR: vImage TARGET IMAGE INIT");
return NULL;
}
// Scale Image
error = vImageScale_ARGB8888(&sourceBuffer, &targetBuffer, nil, kvImageHighQualityResampling);
if(error != kvImageNoError) {
NSLog(@"ERROR: vImage SCALE");
return NULL;
}
// Create Image
CGImageRef targetCGImage = vImageCreateCGImageFromBuffer(&targetBuffer, &format, nil, nil, kvImageNoFlags, &error);
if(error != kvImageNoError) {
NSLog(@"ERROR: vImage CREATE CGImage");
return NULL;
}
UIImage *resizedImage = [UIImage imageWithCGImage:targetCGImage];
free(sourceBuffer.data);
free(targetBuffer.data);
CGColorSpaceRelease(colorspace);
return resizedImage;
}
and I just tested that u recommand me. unfortunately, It doesn't work too.
if I use something wrong, plz tell me
(UIImage *)resizeImage:(UIImage *)originalImage size:(CGSize)size
{
CGSize ceiledSize = CGSizeMake(ceil(size.width), ceil(size.height));
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = 0.5;
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:ceiledSize format:format];
UIImage *resultImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, CGRectMake(0.0, 0.0, ceiledSize.width, ceiledSize.height), originalImage.CGImage);
}];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return resultImage;
}