gpt4 book ai didi

iphone - CGBitmapContext在尝试调整图像大小时创建问题

转载 作者:行者123 更新时间:2023-12-03 17:26:47 25 4
gpt4 key购买 nike

当我尝试创建 CGContextRef 并尝试调整某些图像的大小时遇到​​问题:

有错误

Sun May 16 20:07:18 new-host.home app [7406] <Error>: Unable to create bitmap delegate device
Sun May 16 20:07:18 new-host.home app [7406] <Error>: createBitmapContext: failed to create delegate.

我的代码如下所示

     - (UIImage*) resizeImage:(UIImage*)originalImage withSize:(CGSize)newSize {
CGSize originalSize = originalImage.size;
CGFloat originalAspectRatio = originalSize.width / originalSize.height;

CGImageRef cgImage = nil;
int bitmapWidth = newSize.width;
int bitmapHeight = newSize.height;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, bitmapWidth, bitmapHeight, 8, bitmapWidth * 4, colorspace, kCGImageAlphaPremultipliedLast);



if (context != nil) {
// Flip the coordinate system
//CGContextScaleCTM(context, 1.0, -1.0);
//CGContextTranslateCTM(context, 0.0, -bitmapHeight);

// Black background
CGRect rect = CGRectMake(0, 0, bitmapWidth, bitmapHeight);
CGContextSetRGBFillColor (context, 0, 0, 0, 1);
CGContextFillRect (context, rect);

// Resize box to maintain aspect ratio
if (originalAspectRatio < 1.0) {
rect.origin.y += (rect.size.height - rect.size.width / originalAspectRatio) * 0.5;
rect.size.height = rect.size.width / originalAspectRatio;
} else {
rect.origin.x += (rect.size.width - rect.size.height * originalAspectRatio) * 0.5;
rect.size.width = rect.size.height * originalAspectRatio;
}

CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

// Draw image
CGContextDrawImage (context, rect, [originalImage CGImage]);

// Get image
cgImage = CGBitmapContextCreateImage (context);

// Release context
CGContextRelease(context);
}
CGColorSpaceRelease(colorspace);

UIImage *result = [UIImage imageWithCGImage:cgImage];
CGImageRelease (cgImage);
return result;
}

最佳答案

您不能将 nil 作为 CGBitmapContextCreate 的第一个参数传递。为像素分配一些空间。想想那些糟糕的像素。

关于iphone - CGBitmapContext在尝试调整图像大小时创建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2846070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com