我是iOS开发的新手,在发布之前进行了搜索,但是在我的项目中根本无法弄清楚如何使图像传递工作。我需要将用户从DrawViewController绘制的UIimage传递给Draw2viewController。#import
//DrawViewController.h
@interface DrawViewController : UIViewController {
CGPoint lastPoint;
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat brush;
CGFloat opacity;
BOOL mouseSwiped;
}
@property (weak, nonatomic) IBOutlet UIImageView *tempDrawImage;
@property (weak, nonatomic) IBOutlet UIImageView *mainImage;
- (IBAction)pencilPressed:(id)sender;
- (IBAction)eraserPressed:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *Sentence1;
@property (weak, nonatomic) IBOutlet UIButton *Done;
@end`
------------------ DrawViewController.m ------------------
#import "DrawViewController.h"
#import "Draw2ViewController.h"
#import "SentenceViewController.h"
@interface DrawViewController ()
@end
@implementation DrawViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 10.0;
opacity = 1.0;
[super viewDidLoad];
// Do any additional setup after loading the view.
Draw2ViewController *view2 = [[Draw2ViewController alloc] init];
view2.myImage = _mainImage;
_Sentence1.text = _SentStr;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)pencilPressed:(id)sender {
UIButton * PressedButton = (UIButton*)sender;
switch(PressedButton.tag)
{
case 0:
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
break;
case 1:
red = 105.0/255.0;
green = 105.0/255.0;
blue = 105.0/255.0;
break;
case 2:
red = 255.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
break;
case 3:
red = 0.0/255.0;
green = 0.0/255.0;
blue = 255.0/255.0;
break;
case 4:
red = 102.0/255.0;
green = 204.0/255.0;
blue = 0.0/255.0;
break;
case 5:
red = 102.0/255.0;
green = 255.0/255.0;
blue = 0.0/255.0;
break;
case 6:
red = 51.0/255.0;
green = 204.0/255.0;
blue = 255.0/255.0;
break;
case 7:
red = 160.0/255.0;
green = 82.0/255.0;
blue = 45.0/255.0;
break;
case 8:
red = 255.0/255.0;
green = 102.0/255.0;
blue = 0.0/255.0;
break;
case 9:
red = 255.0/255.0;
green = 255.0/255.0;
blue = 0.0/255.0;
break;
}
}
- (IBAction)eraserPressed:(id)sender {
red = 255.0/255.0;
green = 255.0/255.0;
blue = 255.0/255.0;
opacity = 1.0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempDrawImage.image = nil;
UIGraphicsEndImageContext();
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ImagePass"])
{
Draw2ViewController *D2V = [segue destinationViewController].
D2V.myImage = _mainImage;
}
}
@end
刚刚创建了Draw2viewController,因此只有默认代码
答案 0 :(得分:0)
在Draw2viewController.h文件中添加:
@property (nonatomic, strong) UIImage *myImage;
在您的DrawViewController导入Draw2viewController.h
中,当您以编程方式显示视图时,您可以执行以下操作:
Draw2viewController *view2 = [[Draw2viewController alloc] init];
view2.myImage = YOUR_UIIMAGE_YOU_WANT_PASS;
// Code for presenting/pushing view2 to view hierarchy
或者当您使用segue通过storyboard执行此操作时,请在prepareForSegue中执行:方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"YOURIDENTIFIER"])
{
Draw2viewController *view2 = (Draw2viewController*)[segue destinationViewController].
view2.myImage = YOUR_UIIMAGE_YOU_WANT_PASS;
}
}
答案 1 :(得分:-1)