【问题标题】:Send Screenshot of ViewController to other ViewController将 ViewController 的屏幕截图发送到其他 ViewController
【发布时间】:2015-04-28 16:54:48
【问题描述】:

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

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

  1. 用户从主视图 ViewController 开始。
  2. 单击按钮(标记为 profTap)后,用户将转到 ProfileViewController
  3. ProfileViewController 通过 segue(模态呈现)显示,其背景设置为来自 ViewController 的前一个视图的模糊图像。

如果有人可以向我展示如何将图像发送到 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

【问题讨论】:

  • 您在问两个不同的问题。如何模糊以及如何将最终图像发送到新控制器。每个帖子只问一个问题。
  • 好吧,我实际上是在问四个问题:如何截屏、模糊、发送和设置为背景。我想我真正的问题是如何将“模糊图像”发送到另一个控制器。我想我会编辑我的帖子

标签: ios objective-c oop uiviewcontroller


【解决方案1】:

试试类似的方法

   @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

【讨论】:

  • 谢谢!这很有帮助
猜你喜欢
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
相关资源
最近更新 更多