在另一个不起作用的代码之上画一个图像

时间:2012-08-21 12:29:07

标签: objective-c uiimage cgcontext

我有2张相同尺寸的图像,我需要制作其中第2张高于第1张的3D照片 我有以下方法

的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;

}

我试图在Photo Album中保存生成的图像,但是它调用错误而不能正常工作。这似乎是错误的。

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

2 个答案:

答案 0 :(得分:1)

你永远不需要获取当前的图形上下文然后推送它 - 你只是复制上下文堆栈的顶部。如果它仍然有效会很好,但事实并非如此。移除对UIGraphicsPushContextUIGraphicsPopContext的来电,它按预期工作。

+(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中显示它,以确保它是正确的图像。)

答案 1 :(得分: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];