【问题标题】:Saving UIImageView and overlayed UIView as an image (xcode/iOS)将 UIImageView 和覆盖的 UIView 保存为图像(xcode/iOS)
【发布时间】:2012-10-17 20:56:12
【问题描述】:

我将图像加载到 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

任何帮助将不胜感激。

【问题讨论】:

    标签: objective-c uiview uiimageview


    【解决方案1】:

    你试过了吗:

    UIGraphicsBeginImageContext(rect.size);
    
    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    [customView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:viewImage];
    

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

    【讨论】:

    • 谢谢,您有更完整的代码或使用中的完整示例吗?
    • 我修复了代码中的一个问题,但是:更完整的示例是什么意思?我知道这是非常完整的,因为它会为您提供来自您的两个视图的图像......或者“保存”您的意思是保存到磁盘?
    • 很酷,谢谢,我最终以不同的方式做,但你让我走上了正确的道路:)
    • 感谢答案,但我的 customView 不在 imageView 上方(甚至尝试使用 drawInContext)
    • @SocoM:我想说这应该不是问题——事实上,它和 OP 的情况一样——你试过运行上面的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多