CoreGraphics使用无效上下文绘制drawAtPoint

3

我对核心图形还有点陌生,当我合并两张图片的时候出现了错误,并且第二张图片没有显示出来:

- (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个回答

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大小的上下文。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接