2013-12-10 60 views
0

我是新開發的iOS開發人員,在發佈之前進行搜索,但無法弄清楚如何在我的項目中完成圖像傳遞工作。我需要通過UIImage的用戶從DrawViewController吸引到的Draw2viewController.`#進口將UIimage繪圖傳遞給另一個viewController

//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

你使用s toryboard從DrawViewController切換到Draw2viewController視圖,或者您創建Draw2viewController並呈現它前衛? – Greg

+0

是的,故事板被使用,所以我不能稱之爲父視圖控制器。 (UIImage *)self.parentViewController.mainImage我想...... –

+0

請參閱我的答案中的prepareForSegue選項。 – Greg

回答

-1

您的默認代碼可以嘗試使用一個委託協議。代表的參考文獻可在here找到。或者,您可以使用帶NSNotification的userInfo的NSNotificationCenter包含圖像。 NSNotification的文檔是here

+0

請解釋倒票,以便我從錯誤中學習。 – DrDisc

+0

我們在大多數情況下需要回調實現時使用委託,例如,如果您在執行某些長度操作的位置有NSOperation,那麼您使用委託實現在完成後通知NSOperations委託其結果。在上面的語句中,您不需要委託,因爲它非常簡單,您可以從一個視圖控制器轉換到另一個視圖控制器,您可以在prepareForSegue中將任何值傳遞給其他視圖控制器,方法是獲取對segue的目標視圖控制器的引用並將其設置爲各自的屬性。 – ldindu

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做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; 
     } 
    } 
+0

是爲drawViewController.m中的segue做準備還是必須手動添加代碼? –

+0

您必須將其添加到drawViewController.m。請注意,我在回答中添加了[segue.identifier isEqualToString:@「YOURIDENTIFIER」]。請記住在storyboard中爲您的segue添加標識符名稱,並將YOURIDENTIFIER替換爲您的名稱。 – Greg

+0

- (無效)prepareForSegue:(UIStoryboardSegue *)賽格瑞發件人:(ID)發送方 { 如果([segue.identifier isEqualToString:@ 「ImagePass」]) { Draw2ViewController *視圖2 =(Draw2viewController *)[原因請看destinationViewController] 。 view2.myImage = _mainImage; } }給出一個錯誤「屬性view2找不到對象的類型id ???? –

相關問題