在我的项目中,我使用UIIamgepickercontroller
从库中选择图像并将其加载到UIImageView
。我正在为2个图像做这个,所以我有两个按钮用于每个图像视图,但我不想复制图像选择器的代码两次,我不知道如何实现,以便该方法知道哪个图像视图加载图像成。我想我需要使用按钮标签?但是找不到合适的方法。
这是我的代码:
·H
`#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface LoadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageView;
IBOutlet UIImageView *imageView2;
}
- (IBAction)pick1;
- (IBAction)pick2;
- (void) getImage;
@end`
的.m
#import "LoadViewController.h"
@implementation LoadViewController
UIImage *imageHandle;
- (IBAction)pick2 {
[self getImage];
imageView2.image = imageHandle;
}
- (IBAction)pick1{
[self getImage];
imageView.image = imageHandle;
}
- (void)getImage {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imageHandle = image;
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
@end
我遇到的另一个问题是,如果我注释掉整个方法(void)imagePickerControllerDidCancel
,那么捕获我是选择了图像还是取消了视图的方法似乎不起作用,那么它会取消吗?! ?
我正处于学习这些东西的早期阶段,非常感谢任何帮助!
由于
答案 0 :(得分:1)
您可以使用标记识别按钮(可以在Interface Builder中设置),这是一个任意整数。问题是您拒绝接收发件人(按钮)的引用;而不是- (IBAction)pick1
,而是- (IBAction)pick1:(id)sender
。现在您可以检查发件人的tag
(将发件人强制转换为UIView *,以便编译器了解您正在做的事情)。