1

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

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

我的故事板截图:

在此处输入图像描述

下面是到目前为止我的 PhotoEditViewController 的代码。passPhoto 是加载到 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

任何帮助,将不胜感激。

4

1 回答 1

3

你有没有试过这个:

UIGraphicsBeginImageContext(rect.size);

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

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

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

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

于 2012-10-17T21:26:07.243 回答