1

UIViewがオーバーレイされた(CGRectが内部に描画された)UIImageViewに画像をロードしました。画面に表示されているものを、新しい画像として保存する方法を探しています。ストーリーボードとARCを使用しています。

UIImageViewを含むUIViewControllerがあります。この上に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これは、からを取得する場合とまったく同じアプローチですがUIView、同じコンテキストで2つのビューを描画するだけです。

于 2012-10-17T21:26:07.243 に答える