我一直在搜索其他StackOverflow问题,试图为此找到解决方案,但无济于事。我有以下代码,它试图将2个按钮添加到cameraOverlayView:
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil))
return NO;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[_cancelPicture addTarget:self action:@selector(cancelPicture:) forControlEvents:UIControlEventTouchUpInside];
[_takePic addTarget:self action:@selector(takePic:) forControlEvents:UIControlEventTouchUpInside];
[picker.cameraOverlayView addSubview: _cancelPicture];
[picker.cameraOverlayView addSubview: _takePic];
[self.view bringSubviewToFront:_cancelPicture];
[self.view bringSubviewToFront:_takePic];
_imageView.userInteractionEnabled = YES;
picker.showsCameraControls = NO;
[self presentViewController:picker animated:YES completion:NULL];
return YES;
}
imageView是我的UIImageView。 cancelPicture应该将用户返回到以前的视图控制器,takePic应该保存照片然后再显示它(再次,在另一个视图控制器上)。当我部署代码时,按钮显示,但点击它们什么都不做。我知道我的委托方法是正确实现的,因此我不认为需要在这里复制它们。我也有以下IBActions,但我不确定它们是否有任何好处:
- (IBAction)cancelPicture:(UIButton *)sender {
[self imagePickerControllerDidCancel:_imagePickerController];
}
- (IBAction)takePic:(UIButton *)sender {
[_imagePickerController takePicture];
}
这些操作会连接到我的情节提要中的相应视图控制器。
编辑:委派方法:
// For responding to the user tapping Cancel.
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
[[picker parentViewController] dismissViewControllerAnimated: YES completion: nil];
}
// For responding to the user accepting a newly-captured picture
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
}
_imageView.image = imageToSave;
[[picker parentViewController] dismissViewControllerAnimated: YES completion:nil];
}