首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGBitmapContextCreate:无效数据字节/行

CGBitmapContextCreate:无效数据字节/行
EN

Stack Overflow用户
提问于 2014-06-09 16:26:09
回答 1查看 6K关注 0票数 6

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

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

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

这是我使用的代码:

代码语言:javascript
复制
    -(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; 
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-09 16:50:54

试试这个:

代码语言:javascript
复制
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), 0, colorSpaceInfo, bitmapInfo);    

bitsPerComponent 内存中像素的每个组成部分要使用的位数。例如,对于32位像素格式和RGB颜色空间,可以指定每个组件8位的值。有关支持像素格式的列表,请参阅Quartz 2D编程指南“图形上下文”一章中的“支持像素格式”。

如果没有改进,请张贴CGImageGetBitsPerComponent(imageRef)CGImageGetBytesPerRow(imageRef)的值。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24124546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档