将ViewController的屏幕截图发送到其他ViewController

时间:2015-04-28 16:54:49

标签: ios objective-c oop uiviewcontroller

我想截取 ViewController 的截图,模糊图像,将其发送到 ProfileViewController ,然后将其设置为 ProfileViewController的背景。这应该不是那么困难,因为每个任务都非常简单,我很难找到关于如何执行此操作的正确语法。

我希望这种方式有效:

  1. 用户从 ViewController (主视图)开始。
  2. 点击按钮(标记为 profTap )后,用户将被带到 ProfileViewController
  3. ProfileViewController 通过segue(模态显示)显示,其背景设置为 ViewController 中上一个视图的模糊图像。
  4. 如果有人可以通过代码向我展示如何将图像发送到 ProfileViewController ,我将非常感激。作为参考,下面是我现在的 ViewController 的代码。我相信我正在正确地捕捉图像,我不知道该怎么做才能将它发送到 ProfileViewController。

    #import "ViewController.h"
    #import <QuartzCore/QuartzCore.h>
    #import "ProfileViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    - (IBAction)profTap:(id)sender {
        UIGraphicsBeginImageContext(self.mainView.bounds.size);
        [self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *mainViewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSData *data = UIImagePNGRepresentation(mainViewImage);
    
        // NEXT: blur image, and send to ProfileViewControoler
    
    //    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainViewImage"]];
    //    [self.view addSubview:backgroundView];
    
    }
    
    
    - (IBAction)unwindToHome:(UIStoryboardSegue *)segue {
    
    }
    
    
    @end
    

1 个答案:

答案 0 :(得分:0)

尝试类似的东西

   @interface ViewController ()
    @property(nonatomic, strong) UIImage *backgroundImage;
    @end
    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];


    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)profTap:(id)sender {

}


- (IBAction)unwindToHome:(UIStoryboardSegue *)segue {
     self.backgroundImage = [self takeSnapShot];
      [self performSegueWithIdentifier:@"nameofyoursegue" sender:nil];
}
//function to take a screenshot of your current controller
- (UIImage *)takeSnapShot {
  // Create the image context
    CGRect bounds = self.mainView.bounds;
    UIGraphicsBeginImageContextWithOptions(bounds.size, NO,
                                           self.view.window.screen.scale);
    [self.navigationController.view drawViewHierarchyInRect:bounds
                                         afterScreenUpdates:NO];

  // Get the snapshot
  UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
  // Be nice and clean your mess up
  UIGraphicsEndImageContext();
  return snapshotImage;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([[segue identifier] isEqualToString:@"nameofyoursegue"]) {

    ProfileViewController *profileViewController =
        [segue destinationViewController];
    [profileViewController setBackgroundImage:self.backgroundImage];
  }

}


@end

PhotoViewController

@interface ProfileViewController ()
@property(nonatomic, weak) IBOutlet UIImageView *backgroundImageView;
            @property(nonatomic, strong) UIImage *backgroundImage;
            @end
@implementation ProfileViewController
- (void)viewDidLoad {
  [super viewDidLoad];
 [self.backgroundImageView
        setImage:self.backgroundImage];
}
@end

关于模糊效果,我建议你在ProfileViewController中使用FXBlurView: https://github.com/nicklockwood/FXBlurView