将 ViewController 的屏幕截图发送给其他 ViewController
Send Screenshot of ViewController to other ViewController
我想截图ViewController,把图片模糊,发到个人资料ViewController,然后设置为个人资料ViewController的背景。这不应该那么困难,因为每个任务都非常简单,我只是很难找到正确的语法来执行此操作。
我希望它的工作方式是这样的:
- 用户从 ViewController 主视图开始。
- 单击按钮(标记为 profTap)后,用户将被带到 ProfileViewController.
- ProfileViewController 通过 segue(模态呈现)显示,其背景设置为来自 [= 的前一个视图的模糊图像49=].
如果有人能用代码告诉我如何将图像发送到 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
尝试类似的东西
@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
我想截图ViewController,把图片模糊,发到个人资料ViewController,然后设置为个人资料ViewController的背景。这不应该那么困难,因为每个任务都非常简单,我只是很难找到正确的语法来执行此操作。
我希望它的工作方式是这样的:
- 用户从 ViewController 主视图开始。
- 单击按钮(标记为 profTap)后,用户将被带到 ProfileViewController.
- ProfileViewController 通过 segue(模态呈现)显示,其背景设置为来自 [= 的前一个视图的模糊图像49=].
如果有人能用代码告诉我如何将图像发送到 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
尝试类似的东西
@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