1

我有 2 个相同大小的图像,我需要制作 3d 图片,其中 2nd 高于 1st 我有 UIImage 类扩展,方法如下

+(UIImage*)imageFrom2Images:(UIImage *)img1 with:(UIImage *)img2 {

UIGraphicsBeginImageContext(img1.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);      
[img1 drawAtPoint:CGPointMake(0, 0)];
[img2 drawAtPoint:CGPointMake(0, 0)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext();
UIGraphicsEndImageContext();
return resultingImage;

}

我试图将生成的图像保存在相册中,但它调用错误并且不起作用..似乎这种方法是错误的。

   UIImage*t1=[self.mw.imageFIFO lastObject];
   UIImage* test=[UIImage imageFrom2Images:self.imageView.image with:t1];
   UIImageWriteToSavedPhotosAlbum(test, self,
          @selector(image:didFinishSavingWithError:contextInfo:), nil);

我收到的错误消息是:

 Error Domain=ALAssetsLibraryErrorDomain Code=-3304 "Failed to encode image for saved photos." 
 UserInfo=0x9398960 {NSUnderlyingError=0x935e760 "Failed to encode image for saved photos.", 
 NSLocalizedDescription=Failed to encode image for saved photos.}
4

2 回答 2

1

您永远不需要获取当前图形上下文然后推送它 - 您只是在复制上下文堆栈的顶部。如果它仍然有效,那就太好了,但它没有。UIGraphicsPushContext删除对and的调用UIGraphicsPopContext,它按预期工作。

+(UIImage*)imageFrom2Images:(UIImage *)img1 with:(UIImage *)img2 {
    UIGraphicsBeginImageContext(img1.size);
    [img1 drawAtPoint:CGPointMake(0, 0)];
    [img2 drawAtPoint:CGPointMake(0, 0)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

要在将来检测这些问题,请检查imageFrom2Images. 正如最初实现nil的那样,它返回 ,因此以下调用不知道该做什么也就不足为奇了。(如果它返回了一个图像对象,下一步就是在 UIImageView 中显示它,以确保它是正确的图像。)

于 2012-08-21T22:54:02.367 回答
0

这个方法应该这样做:

-(UIImage *)drawFirstImage:(UIImage*)firstImage afterSecondImage:(UIImage *)secondImage
{

    float finalWidth=MAX(firstImage.size.width,secondImage.size.width);
    float finalHeight=firstImage.size.height +  secondImage.size.height;

    CGSize finalSize=CGSizeMake(finalWidth, finalHeight);
    UIGraphicsBeginImageContext(finalSize);

    [firstImage drawInRect:CGRectMake(0, 0, firstImage.size.width, firstImage.size.height)];
    [secondImage drawInRect:CGRectMake(0, firstImage.size.height, secondImage.size.width, secondImage.size.height)];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return resultImage;
}

您可以将其用作:

 UIImage *resultImage= [self drawFirstImage:image1 afterSecondImage:image2];

这是一个类别实现:

UIImage+MyExtensions.h

#import <UIKit/UIKit.h>

@interface UIImage (MyExtensions)
-(UIImage *)attachImageBelow:(UIImage *)secondImage;
@end

UIImage+MyExtensions.m

#import "UIImage+MyExtensions.h"

@implementation UIImage (MyExtensions)

-(UIImage *)attachImageBelow:(UIImage *)secondImage{
    float finalWidth=MAX(self.size.width,secondImage.size.width);
    float finalHeight=self.size.height +  secondImage.size.height;

    CGSize finalSize=CGSizeMake(finalWidth, finalHeight);
    UIGraphicsBeginImageContext(finalSize);

    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    [secondImage drawInRect:CGRectMake(0, self.size.height, secondImage.size.width, secondImage.size.height)];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
}

你可以像这样使用它:

#import "UIImage+MyExtensions.h"
UIImage *resultImage= [image1 attachImageBelow:image2];
于 2013-06-18T13:35:15.713 回答