ios - 尝试将两个 UIImage 合并到 UIImageView 时出错

标签 ios objective-c uiimageview uiimage

我正在尝试将几张照片 (UIImages) 合并到 ImageView 中。我正在使用这种方法:

- (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 上。是否有可行的解决方案?谢谢。

最佳答案

使用下面的代码:

- (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
}

关于ios - 尝试将两个 UIImage 合并到 UIImageView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21186372/

相关文章:

ios - NSFileManager 不创建文件夹

ios - 使用在 UIImageView 上设置的启动图像

ios - 如何在 Swift 3 中将数据转换为 Int?

ios - 使用自定义类以编程方式创建 UIView (Swift 4)

iphone - 圆弧 : sending nil to an object doesn't call its dealloc immediately

ios - 如何使用 AFNetworking 允许 https 连接?

ios - 是否可以改变图像的颜色?

ios - 在 iOS 7 中有没有办法在标签栏上显示图像?

ios - 如何更改 UIBarButtonItem 的选择区域?

ios - 无法从 Parse 对象检索 createdAt 值