CGContext仅在模拟器中出错?

时间:2013-08-01 04:12:29

标签: ios uiimageview uiimage cgcontext

在测试我的应用程序是否存在任何错误时,我注意到在模拟器中调用我的一个函数将图像设置为UIImageView时,会抛出一堆CGContext错误并且图像永远不会被设置。但是在我的设备上(我已经在iPhone 5,iPhone 3GS和iPad 3上测试过),这些CGContext错误都没有出现,图像设置得很好。

无论如何,我寻找任何可以实现这一点的CGContext代码,以下是涉及CGContext的唯一方法:

- (void)setCropRect:(CGRect)cropRect
{
    if(!CGRectEqualToRect(_cropRect,cropRect)){
        _cropRect = cropRect;
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.f);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [[UIColor blackColor] setFill];
        UIRectFill(self.bounds);
        CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor);
        CGContextStrokeRect(context, _cropRect);
        [[UIColor clearColor] setFill];
        UIRectFill(CGRectInset(_cropRect, 1, 1));
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
    }
}

- (CGImageRef)newScaledImage:(CGImageRef)source withOrientation:(UIImageOrientation)orientation toSize:(CGSize)size withQuality:(CGInterpolationQuality)quality
{
    CGSize srcSize = size;
    CGFloat rotation = 0.0;

    switch(orientation)
    {
        case UIImageOrientationUp: {
            rotation = 0;
        } break;
        case UIImageOrientationDown: {
            rotation = M_PI;
        } break;
        case UIImageOrientationLeft:{
            rotation = M_PI_2;
            srcSize = CGSizeMake(size.height, size.width);
        } break;
        case UIImageOrientationRight: {
            rotation = -M_PI_2;
            srcSize = CGSizeMake(size.height, size.width);
        } break;
        default:
            break;
    }

    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 size.width,
                                                 size.height,
                                                 8, //CGImageGetBitsPerComponent(source),
                                                 0,
                                                 CGImageGetColorSpace(source),
                                                 kCGImageAlphaNoneSkipFirst//CGImageGetBitmapInfo(source)
                                                 );

    CGContextSetInterpolationQuality(context, quality);
    CGContextTranslateCTM(context,  size.width/2,  size.height/2);
    CGContextRotateCTM(context,rotation);

    CGContextDrawImage(context, CGRectMake(-srcSize.width/2 ,
                                           -srcSize.height/2,
                                           srcSize.width,
                                           srcSize.height),
                       source);

    CGImageRef resultRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    return resultRef;
}

- (CGImageRef)newTransformedImage:(CGAffineTransform)transform
                      sourceImage:(CGImageRef)sourceImage
                       sourceSize:(CGSize)sourceSize
                sourceOrientation:(UIImageOrientation)sourceOrientation
                      outputWidth:(CGFloat)outputWidth
                         cropSize:(CGSize)cropSize
                    imageViewSize:(CGSize)imageViewSize
{
    CGImageRef source = [self newScaledImage:sourceImage
                             withOrientation:sourceOrientation
                                      toSize:sourceSize
                                 withQuality:kCGInterpolationNone];

    CGFloat aspect = cropSize.height/cropSize.width;
    CGSize outputSize = CGSizeMake(outputWidth, outputWidth*aspect);

    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 outputSize.width,
                                                 outputSize.height,
                                                 CGImageGetBitsPerComponent(source),
                                                 0,
                                                 CGImageGetColorSpace(source),
                                                 CGImageGetBitmapInfo(source));
    CGContextSetFillColorWithColor(context,  [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, outputSize.width, outputSize.height));

    CGAffineTransform uiCoords = CGAffineTransformMakeScale(outputSize.width/cropSize.width,
                                                            outputSize.height/cropSize.height);
    uiCoords = CGAffineTransformTranslate(uiCoords, cropSize.width/2.0, cropSize.height/2.0);
    uiCoords = CGAffineTransformScale(uiCoords, 1.0, -1.0);
    CGContextConcatCTM(context, uiCoords);

    CGContextConcatCTM(context, transform);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(-imageViewSize.width/2.0,
                                           -imageViewSize.height/2.0,
                                           imageViewSize.width,
                                           imageViewSize.height)
                       ,source);

    CGImageRef resultRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGImageRelease(source);
    return resultRef;
}

这些是CGContext错误:

Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaNoneSkipFirst; 1504 bytes/row.
Jul 31 23:37:37 app[10190] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextTranslateCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextRotateCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextDrawImage: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreateImage: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreate: unsupported parameter combination: 0 integer bits/component; 0 bits/pixel; 0-component color space; kCGImageAlphaNone; 0 bytes/row.
Jul 31 23:37:37 app[10190] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextFillRects: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextConcatCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextConcatCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextScaleCTM: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGContextDrawImage: invalid context 0x0
Jul 31 23:37:37 app[10190] <Error>: CGBitmapContextCreateImage: invalid context 0x0

这是我的应用程序中使用CGContexts的唯一方法。但就像我说的那样,这只发生在模拟器中,而不是我测试过的设备上。

无论如何,这有什么特别的原因,或者我有什么方法可以解决这个问题?

全部谢谢!

CGContextRef context = CGBitmapContextCreate(NULL,
                                                 size.width,
                                                 size.height,
                                                 CGImageGetBitsPerComponent(source), //8,
                                                 0,
                                                 CGImageGetColorSpace(source),
                                                 CGImageGetBitmapInfo //kCGImageAlphaNoneSkipFirst(source)
                                                 );

2 个答案:

答案 0 :(得分:1)

最有可能的是,您对CGBitmapContextCreate的第一次调用失败了,因为您要求它做一些不能做的事情。您可以在第一条错误消息中看到:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaNoneSkipFirst; 1504 bytes/row.

CGBitmapContextCreate返回NULL,其余的故障从那里级联。 (你不能对NULL上下文进行操作,并且在NULL上下文上调用CGBitmapContextGetImage()会给你一个NULL图像,你同样也无法做任何有用的事情。)

CGBitmapContextCreate州的文档:

  

有关支持的像素格式列表,请参阅“Graphics Contexts”Quartz 2D Programming Guide一章中的“支持的像素格式”。

这是该列表的direct link

答案 1 :(得分:0)

您对CGBitmapContextCreate的两次调用都使用不受支持的位/组件 - 位/像素组合(如CGContext错误告诉您的那样)。

请参阅Quartz 2D Programming Guide中的表2.1,了解支持的像素格式列表