gpt4 book ai didi

ios - 我如何在 View Controller 之间传递 UIImageView?

转载 作者:行者123 更新时间:2023-11-29 02:38:52 25 4
gpt4 key购买 nike

我正在尝试从 View Controller 中的相机胶卷中选择一张图像,然后将其传递/在另一个 View Controller 中显示该图像。为什么 ImageView 没有通过?

当登陆 FirstViewController 时,我在导航栏中有一个到 SecondViewController 的 segue。

//FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

//FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController () <SeconViewControllerDelegate>

@end

@implementation FirstViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - SecondViewControllerDelegate
- (void)didDismissWithImageView:(UIImageView *)imageView
{
self.imageView = imageView;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"addPhoto"]) {
SecondViewController *secondVC = segue.destinationViewController;
secondVC.delegate = self;
}
}

@end

//SecondViewController.h

#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>

@protocol PhotoPickerDelegate <NSObject>

- (void)didDismissWithImageView:(UIImageView *)imageView;

@end

@interface SecondViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) id<PhotoPickerDelegate> delegate;

@property (strong, nonatomic) UIImageView *imageView;

- (IBAction)useCameraRoll:(id)sender;

@end

//SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (IBAction)useCameraRoll:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}


#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

UIImage *image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;

}

if ([self.delegate respondsToSelector:@selector(didDismissWithImageView:)]) {
[self.delegate didDismissWithImageView:_imageView];
}

[self.navigationController popViewControllerAnimated:YES];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}

最佳答案

如果我们在这里讨论的只是一个图像,那么让我们为该图像创建一个属性。

@property (nonatomic, strong) UIImage *imageIWant; 

一旦我们有了图像属性,我们就可以通过调用[UIImage imageWithData:imageData];来设置您想要的图像,或者从我们的Plist中的文件中设置它。我不建议保存 imageView 并在 Controller 之间传递该 View ,因为在我们的 MVC 模型中, View 和模型是分开的。归根结底,您需要的是模型中的图像,而不是 View 元素。

关于ios - 我如何在 View Controller 之间传递 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26025639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com