的截图发送到其他

的截图发送到其他

本文介绍了将 ViewController 的截图发送到其他 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to take a screenshot of ViewController, blur the image, send it to ProfileViewController, then set it as the background of ProfileViewController. This should not be that difficult as each task is pretty straightforward, I am just having difficulty finding the correct syntax as to how to do this.

我希望它的工作方式是这样的:

The way I want this to work is such:

  1. 用户从主视图 ViewController 开始.
  2. 点击按钮(标记为profTap)后,用户将被带到ProfileViewController.
  3. ProfileViewController 通过 segue(模态呈现)显示,其背景设置为 ViewController 上一个视图的模糊图像.
  1. The user starts on ViewController, the main view.
  2. Upon the click of a button (labeled profTap) the user is taken to ProfileViewController.
  3. ProfileViewController is displayed through a segue (present modally) with its background set as a blurred image of the previous view from ViewController.

如果有人可以向我展示如何将图像发送到 ProfileViewController 的代码,我将不胜感激.作为参考,下面是我现在拥有的 ViewController 代码.我相信我正确地捕获了图像,我不确定从这里做什么才能将其发送到 ProfileViewController.

If someone could show me with code how to send the image to ProfileViewController I would be most appreciative. For reference, here is my code below that I have for now of the ViewController. I believe I am capturing the image correctly, I am jsut not sure what to do from here to send it to the 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

照片视图控制器

@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

About the blur effect, I advice you to use FXBlurView in your ProfileViewController :https://github.com/nicklockwood/FXBlurView

这篇关于将 ViewController 的截图发送到其他 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:05