gpt4 book ai didi

ios - 在 iOS 的另一个 View 中打开相机

转载 作者:行者123 更新时间:2023-11-28 22:15:46 24 4
gpt4 key购买 nike

如何从我的 MainController.m 类中调用此方法并将图像传递给我的 MainController

CameraController.m

- (void) openCamera {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourcetype = UIImagePickerContrllerSourceTypeCamera;
[self presentViewContrller:picker animated:YES completion:nil];

}

我还是 iOS 开发的新手,是否可以使用委托(delegate)?

编辑:CameraController 没有 Nib 。

最佳答案

CameraController.h 中添加这些行:

@protocol CameraControllerDelegate <NSObject>
- (void)didFinishCapturingImage:(UIImage*)image;
@end

并在 CameraController 类中添加一个属性(在 CameraController.h 中)

@property (nonatomic, strong) id<CameraControllerDelegate> delegate;

MainController.h中,在接口(interface)实现中添加协议(protocol);像这样:

@interface MainController : NSObject <CameraControllerDelegate>

在从 MainController.m 呈现/推送 CameraController 之前,添加以下行:

cameraController.delegate = self;

MainController.m中实现didFinishCapturingImage方法。

- (void)didFinishCapturingImage:(UIImage*)image {
//your logic
}

在 Image Picker Controller Delegate 中,添加这一行:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion: Nil];
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//this will send delegate callback to MainController
[self.delegate didFinishCapturingImage:image];
}


编辑:第二个解决方案:

CameraController.h 中创建一个属性以保留 MainController 对象的实例:

@property (nonatomic, strong) MainController *mainController;

将 self 分配给 CameraController 实例的属性 mainController,您在其中使用 CameraController:

cameraController.mainController = self;

编辑你的openCamera方法:

- (void) openCamera {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self.mainController; //important line
picker.sourcetype = UIImagePickerContrllerSourceTypeCamera;
[self.mainController presentViewContrller:picker animated:YES completion:nil];
}

并在MainController.m

中实现以下方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//your logic
}

不要忘记在 MainController.h 中包含 UIImagePickerControllerDelegate

无需创建协议(protocol)和实现协议(protocol)方法..

关于ios - 在 iOS 的另一个 View 中打开相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21745764/

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