我有一个图像加载到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
任何帮助都将不胜感激。
答案 0 :(得分:3)
你试过这个:
UIGraphicsBeginImageContext(rect.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[customView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:viewImage];
这与从UIImage
获取UIView
的方法相同,只是在同一上下文中绘制两个视图。