我正在尝试将几张照片(UIImages)合并到图像视图中。我正在使用这种方法:
- (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2{
CGSize size = CGSizeMake(finalPhoto.image.size.width, finalPhoto.image.size.height);
UIGraphicsBeginImageContext(size);
CGPoint pointImg1 = CGPointMake(0,0);
[img drawAtPoint:pointImg1];
CGPoint pointImg2 = CGPointMake(0,0);
[img2 drawAtPoint: pointImg2];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
问题是它无法进行合并,因为它没有更正点(我猜......)这是日志:
Jan 17 09:35:20 <Error>: CGContextSaveGState: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextSetBlendMode: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextSetAlpha: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextTranslateCTM: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextScaleCTM: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextConcatCTM: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextDrawImage: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextRestoreGState: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextSaveGState: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextSetBlendMode: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextSetAlpha: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextTranslateCTM: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextScaleCTM: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextConcatCTM: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextDrawImage: invalid context 0x0
Jan 17 09:35:20 <Error>: CGContextRestoreGState: invalid context 0x0
这个问题发生在iOS 6和iOS 7上。有没有可行的解决方案?感谢。
答案 0 :(得分:1)
使用以下代码:
- (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2{
//merge two images for this code
CGSize newSize = CGSizeMake(512,512); // Set your bottom image size
UIGraphicsBeginImageContext(newSize);
// Use existing opacity as is
[img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];///1st image set frame
// Apply supplied opacity if applicable
[img2 drawInRect:CGRectMake(165,332,165,120) blendMode:kCGBlendModeNormal alpha:1];//2nd image set frame with alpha value
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage; //return UIImage
}
答案 1 :(得分:0)
我的猜测是你的图像是零指针,所以你用来创建上下文的矩形是空的。在创建上下文之前检查大小的值:
NSLog(@"Size = %@", NSStringFromCGSize(size));