gpt4 book ai didi

ios - CGBitmapContextCreate : invalid data bytes/row

转载 作者:可可西里 更新时间:2023-11-01 06:24:33 25 4
gpt4 key购买 nike

我试图在将图像显示给用户之前调整图像的大小。

我得到的错误是错误:CGBitmapContextCreate:无效数据字节/行:对于 8 个整数位/组件、3 个组件、kCGImageAlphaNoneSkipFirst,应该至少为 5504。

我还收到错误:CGContextDrawImage:无效上下文 0x0。

这是我正在使用的代码:

    -(UIImage*)imageByScalingToSize:(CGRect)target :(UIImage*)old
{
UIImage* sourceImage = old;
CGFloat targetWidth = target.size.width + 10;
CGFloat targetHeight = target.size.height;
CGFloat x = target.origin.x;
CGFloat y = target.origin.y - 105;

CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
} CGContextRef bitmap;

if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}

if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
}
else if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
}
else if (sourceImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
}
else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-180.));
}

CGContextDrawImage(bitmap, CGRectMake(x, y, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);
CGImageRelease(ref);

return newImage;
}

最佳答案

试试这个:

bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), 0, colorSpaceInfo, bitmapInfo);    

bitsPerComponent

The number of bits to use for each component of a pixel in memory. For example, for a 32-bit pixel format and an RGB color space, you would specify a value of 8 bits per component. For the list of supported pixel formats, see “Supported Pixel Formats” in the “Graphics Contexts” chapter of Quartz 2D Programming Guide.

如果这不能改善情况,请发布 CGImageGetBitsPerComponent(imageRef)CGImageGetBytesPerRow(imageRef) 的值。

关于ios - CGBitmapContextCreate : invalid data bytes/row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24124546/

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