iDev/iOS Dev

SpriteKit에서 현재 화면 캡쳐 및 트위터 공유하는 방법

KraZYeom 2014. 2. 12. 07:52
반응형

게임이 끝나고 iOS 내장된 트위터를 통해서 점수를 보낼 때, 현재 화면을 캡쳐해서 UIImage로 저장하는 방법이다. 



- (void)onShareClicked:(id)sender {

  NSString *scoreString = [NSString stringWithFormat:@"Just scored %ld.", (long)_score];


  SLComposeViewController *tvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

  [tvc setInitialText:scoreString];

// 여기서 부터가 현재 화면을 UIImage로 캡쳐 하는 방법.

  UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);

  [self.view drawViewHierarchyInRect:self.frame afterScreenUpdates:YES];

  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  [tvc addImage:image];

  

  UIViewController *viewController = [UIViewController new];

  [viewController presentViewController:tvc animated:YES completion:nil];        

// 컴플리트핸들러 사용 안해서 뷰를 dismiss 안하면 터치 안됨. -_ -; 삽질 함. 

  tvc.completionHandler = ^(SLComposeViewControllerResult result){

  

    [viewController dismissViewControllerAnimated:YES completion:nil];

  };

  

  [self.view addSubview:viewController.view];

}


반응형