CoreGraphics drawAtPoint具有无效的上下文

时间:2013-07-11 13:45:36

标签: objective-c core-graphics

我对核心图形有点新,我保留了一个错误,当合并它们时第二张图片没有出现:

- (UIImage*)imageByCombiningImage:(UIImage*)gfirstImage withImage:(UIImage*)gsecondImage atPositionX:(int)xPosition withPositionY:(int)yPosition{

    UIImage *firstImage = nil;
    UIImage *secondImage = nil;

    firstImage = gfirstImage;
    secondImage = gsecondImage;

    // int ratio = secondImage.size.height/secondImage.size.width;
    // int newWidth = firstImage.size.width/3;
    // int newHeight = (firstImage.size.height/3)*ratio;
    CGSize scaledSize = CGSizeMake(firstImage.size.width, firstImage.size.height);
    CGSize badgeScaledSize = scaledSize;
    if(firstImage.size.width > 500){
        scaledSize = CGSizeMake(firstImage.size.width/4, firstImage.size.height/3);
    }

    if(firstImage.size.width < firstImage.size.height){
        badgeScaledSize = CGSizeMake((firstImage.size.width/4)*prevPinchScale, (firstImage.size.height/4)*prevPinchScale);
    }

    if(firstImage.size.width > firstImage.size.height){
        badgeScaledSize = CGSizeMake((firstImage.size.width/4)*prevPinchScale, (firstImage.size.height/4)*prevPinchScale);
    }



    secondImage = [secondImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:badgeScaledSize interpolationQuality:0.5];

    CGSize newImageSize = CGSizeMake(firstImage.size.width, firstImage.size.height);

    UIGraphicsBeginImageContext(newImageSize);

    NSLog(@"first image size width: %f, size height: %f", firstImage.size.width, firstImage.size.height);
    NSLog(@"first image size width: %f, size height: %f", newImageSize.width, newImageSize.height);

    [firstImage drawAtPoint:CGPointMake(0,
                                        0)];


    [secondImage drawAtPoint:CGPointMake(xPosition,
                                         yPosition)];

    UIImage *image = nil;
    image = UIGraphicsGetImageFromCurrentImageContext();

    NSLog(@"got image of width: %f and height: %f", image.size.width, image.size.height);

    UIGraphicsEndImageContext();


    return image;
}

错误如下:

 <Error>: CGContextConcatCTM: invalid context 0x0
 <Error>: CGContextSetInterpolationQuality: invalid context 0x0
 <Error>: CGContextDrawImage: invalid context 0x0
 <Error>: CGBitmapContextCreateImage: invalid context 0x0

1 个答案:

答案 0 :(得分:2)

这些错误实际上并没有从您的代码中抛出,而是在UIImage + Resize.m中我假设您正在使用。如果删除行

,则代码运行正常
secondImage = [secondImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:badgeScaledSize interpolationQuality:0.5];

如果您查看该方法,您会发现它调用另一种方法,使得四次调用都会出错。

- (UIImage *)resizedImage:(CGSize)newSize
            transform:(CGAffineTransform)transform
       drawTransposed:(BOOL)transpose
 interpolationQuality:(CGInterpolationQuality)quality {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
CGImageRef imageRef = self.CGImage;

// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            newRect.size.width,
                                            newRect.size.height,
                                            CGImageGetBitsPerComponent(imageRef),
                                            0,
                                            CGImageGetColorSpace(imageRef),
                                            CGImageGetBitmapInfo(imageRef));

// Rotate and/or flip the image if required by its orientation
CGContextConcatCTM(bitmap, transform);

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, quality);

// Draw into the context; this scales the image
CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

// Clean up
CGContextRelease(bitmap);
CGImageRelease(newImageRef);

return newImage;
}

似乎值位图无效。我会检查以确保在CGBitmapContextCreate调用上,您传递实际值而不是创建大小为0x0的上下文。