Objective c 将UIImageView和覆盖的UIView另存为图像(xcode/iOS)

Objective c 将UIImageView和覆盖的UIView另存为图像(xcode/iOS),objective-c,uiview,uiimageview,Objective C,Uiview,Uiimageview,我将一个图像加载到UIImageView中,该视图覆盖了一个UIView(其中绘制了一个CGRect),我正在寻找一种将屏幕上显示的内容保存为新图像的方法。我正在使用故事板和ARC 我有一个UIViewController,它包含UIImageView。UIView显示在屏幕上方,并在用户触摸屏幕的位置绘制一个圆圈。这一切都很好,但现在我想保存显示为图像(JPG)的内容 我的故事板截图: 下面是我的PhotoEditViewController到目前为止的代码。passedPhoto是加载到U

我将一个图像加载到UIImageView中,该视图覆盖了一个UIView(其中绘制了一个CGRect),我正在寻找一种将屏幕上显示的内容保存为新图像的方法。我正在使用故事板和ARC

我有一个UIViewController,它包含UIImageView。UIView显示在屏幕上方,并在用户触摸屏幕的位置绘制一个圆圈。这一切都很好,但现在我想保存显示为图像(JPG)的内容

我的故事板截图:

下面是我的PhotoEditViewController到目前为止的代码。passedPhoto是加载到UIImageView中的照片

#import "PhotoEditViewController.h"
@interface PhotoEditViewController ()
@end

@implementation PhotoEditViewController
@synthesize selectedPhoto = _selectedPhoto;
@synthesize backButton;
@synthesize savePhoto;

- (void)viewDidLoad {
    [super viewDidLoad];
    [passedPhoto setImage:_selectedPhoto];
}

- (void)viewDidUnload {
    [self setBackButton:nil];
    [self setSavePhoto:nil];
    [super viewDidUnload];
}

- (IBAction)savePhoto:(id)sender{
    NSLog(@"save photo");
    // this is where it needs to happen!
}

@end
下面是我的PhotoEditView中的代码,它处理圆覆盖的创建:

#import "PhotoEditView.h"

@implementation PhotoEditView
@synthesize myPoint = _myPoint;

- (void)setMyPoint:(CGPoint)myPoint {
    _myPoint = myPoint;
    self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.01];
    [self setNeedsDisplay];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    return self;
}

-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
    NSSet *allTouches = [event allTouches];
    switch ([allTouches count]){
        case 1: {
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
            CGPoint point = [touch locationInView:self];
            NSLog(@"x=%f", point.x);
            NSLog(@"y=%f", point.y);    
            self.myPoint = point;
        }
        break;
    }
}

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 4.0);
    CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.5);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1);
    CGContextStrokePath(ctx);
    CGRect circlePoint = CGRectMake(self.myPoint.x - 50, self.myPoint.y - 50, 100.0, 100.0);
    CGContextStrokeEllipseInRect(ctx, circlePoint);
}

@end
任何帮助都将不胜感激。

您是否尝试过以下方法:

UIGraphicsBeginImageContext(rect.size);

[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[customView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imgView = [[UIImageView alloc] initWithImage:viewImage];

这与从
UIView
获取
UIImage
的方法相同,只是在同一上下文中绘制了两个视图。

谢谢,您是否有更完整的代码或完整的示例在使用?我修复了代码中的一个问题,但是:您所说的更完整的示例是什么意思?我理解这是相当完整的,因为它将给你一个图像从你的两个观点。。。或者说“保存”是指保存到磁盘?很酷,谢谢,我最后用了另一种方式,但你让我走上了正确的道路:)答案是thx,但我的自定义视图在imageView上面,而不是在里面(甚至尝试了drawInContext)@SocoM:我想说这应该不是问题——事实上,这和OP的情况一样——你试过运行上面的代码吗?