Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 尝试将两个UIImages合并到UIImageView时出错_Ios_Objective C_Uiimageview_Uiimage - Fatal编程技术网

Ios 尝试将两个UIImages合并到UIImageView时出错

Ios 尝试将两个UIImages合并到UIImageView时出错,ios,objective-c,uiimageview,uiimage,Ios,Objective C,Uiimageview,Uiimage,我正在尝试将几张照片(UIImages)合并到一个图像视图中。我正在使用这种方法: - (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2{ CGSize size = CGSizeMake(finalPhoto.image.size.width, finalPhoto.image.size.height); UIGraphicsBeginImageContext(size); C

我正在尝试将几张照片(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
Jan 17 09:35:20:CGContextSaveGState:无效上下文0x0
Jan 17 09:35:20:CGContextSetBlendMode:无效上下文0x0
Jan 17 09:35:20:CGContextSetAlpha:无效上下文0x0
Jan 17 09:35:20:CGContextTranslateCm:无效的上下文0x0
Jan 17 09:35:20:CGContextScaleCTM:无效的上下文0x0
Jan 17 09:35:20:CGContextConcatCTM:无效的上下文0x0
Jan 17 09:35:20:CGContextDrawImage:无效上下文0x0
Jan 17 09:35:20:CGContextRestoreGState:无效上下文0x0
Jan 17 09:35:20:CGContextSaveGState:无效的上下文0x0
Jan 17 09:35:20:CGContextSetBlendMode:无效上下文0x0
Jan 17 09:35:20:CGContextSetAlpha:无效上下文0x0
Jan 17 09:35:20:CGContextTranslateCm:无效的上下文0x0
Jan 17 09:35:20:CGContextScaleCTM:无效的上下文0x0
Jan 17 09:35:20:CGContextConcatCTM:无效的上下文0x0
Jan 17 09:35:20:CGContextDrawImage:无效上下文0x0
Jan 17 09:35:20:CGContextRestoreGState:无效上下文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
}

我猜你的图像是零指针,所以你用来创建上下文的矩形是空的。在创建上下文之前,请检查大小的值:

NSLog(@"Size = %@", NSStringFromCGSize(size));

什么是
NSString*savedImagePath=[DocumentsDirectoryStringByAppendingPathComponent:@“savedImage.png”]?我没有使用来自文件的图像,我使用的是2
@property(非原子)UIImage*firstPhotoImage@属性(非原子)UIImage*secondPhotoImage取自camera.NSString*savedImagePath=[DocumentsDirectoryStringByAppendingPathComponent:@“savedImage.png”];这是为了将合并图像保存在您的文档目录文件中application@Vergmort对你有用吗?或者你有什么问题吗?很好。就一个简单的问题。在合并它们之前,我可以将它们定位在不同的位置上吗?(左边的IMG1和右边的图像2,因为我在拍摄照片时把它们放在中间,然后将它们合并)@ VrgMrt,可以通过使用DrWrRunt:CGRectMake(x,y,宽度,高度)改变两个图像的值来设置它的位置或帧;