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
4

1 回答 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 的上下文。

于 2013-07-11T14:38:15.673 回答