2

我需要将两张图片合并为一张图片,这是我的代码:

-(UIImage*)mergeImage:(UIImage*)mask overImage:(UIImage*)source inSize:(CGSize)size
{
    //Capture image context ref

    UIImageView *totalimage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

    UIImageView *firstImage=[[UIImageView alloc] initWithImage:mask];
    UIImageView *secondImage=[[UIImageView alloc] initWithImage:source];

    [totalimage addSubview:firstImage];
    [totalimage addSubview:secondImage];

    UIGraphicsBeginImageContext(totalimage.bounds.size);
    [totalimage.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Draw images onto the context
    [source drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)]; 
    [mask drawInRect:CGRectMake(0, 0, mask.size.width, mask.size.height)]; 

    return viewImage;

}

我称这个方法如下:

UIImage *totalImage = [self mergeImage:self.Apicimage overImage:questionImage inSize:CGSizeMake(100, 100)];

但在执行时我得到这个输出:

Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSaveGState: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetBlendMode: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetAlpha: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextTranslateCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextScaleCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextConcatCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextDrawImage: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextRestoreGState: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSaveGState: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetBlendMode: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetAlpha: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextTranslateCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextScaleCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextConcatCTM: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextDrawImage: invalid context 0x0
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextRestoreGState: invalid context 0x0

谁能指导我。如何合并两个图像?

4

1 回答 1

3

你的代码

[source drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)]; 
    [mask drawInRect:CGRectMake(0, 0, mask.size.width, mask.size.height)]; 

应该写在前面

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2012-06-14T10:56:41.220 回答