如何將手機螢幕截圖分享到臉書︰
設定Navigation Bar按鈕
- (void)viewDidLoad { UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(saveScreenshotToPhotosAlbum:)]; //[self.navigationItem setRightBarButtonItem:shareBtn]; [self.navigationItem setRightBarButtonItems:[self.navigationItem.rightBarButtonItems arrayByAddingObject:shareBtn]]; }
按鈕事件
- (void)saveScreenshotToPhotosAlbum:(UIView *)view { if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){ self.mySLComposerSheet = [[SLComposeViewController alloc] init]; self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; [self.mySLComposerSheet setInitialText:@"Some Text"]; [self.mySLComposerSheet addImage:[self captureView:self.view]]; [self presentViewController:self.mySLComposerSheet animated:YES completion:nil]; } }
螢幕截圖
- (UIImage*)captureView:(UIView *)view { CGRect rect = [[UIScreen mainScreen] bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [view.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 以下是為了刪除上方因NavigationBar造成的空白 CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height; rect.size.height = self.view.frame.size.height - navigationBarHeight; //NSLog(@"rect.size.height=%f", rect.size.height); UIGraphicsBeginImageContext(rect.size); [img drawAtPoint:CGPointMake(0, navigationBarHeight * -1)]; UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return croppedImage; }
參考︰
http://stackoverflow.com/questions/16241377/share-screenshot-on-facebook
留言
張貼留言