【发布时间】:2014-05-21 23:23:32
【问题描述】:
在相机工作流程中,照片被捕获并在下一个屏幕上,我们称之为选择屏幕,您可以选择是要使用这张照片还是重新拍摄。
我怎么知道相机何时进入预览视图?
我的问题是我添加了一个按钮来访问相机胶卷,效果很好。障碍是,当拍照并进入预览视图(2.相机视图)时,按钮隐藏了“使用照片”选项。所以我无法选择它。我想在进入预览视图时隐藏按钮,或者只是避开预览视图。
在我的代码下面
CamViewScreen.h
#import <UIKit/UIKit.h>
#import "CameraViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface CamViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) UIImage *lastTakenImage;
- (IBAction)takePhoto:(id)sender;
- (IBAction)selectPhoto:(id)sender;
@end
CamViewScreen.m
#import "CamViewController.h"
@interface CamViewController ()
@end
@implementation CamViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
int isAction = 0; // Photo, 1: CameraRoll, 2: Cancel
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
isAction = 0;
[self cameraRoll];
}
-(void)viewDidAppear:(BOOL)animated {
// isAction = 0 Photo, 1: CameraRoll, 2: Cancel
DLog(@"###### isAction> %d", isAction);
switch (isAction) {
case 1:
[self selectPhoto:nil];
break;
case 2:
[self dismissViewControllerAnimated:NO completion:nil];
break;
default:
[self takePhoto:nil];
break;
}
}
- (IBAction)takePhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraOverlayView = [self addCameraRollButton]; // suggestion from omz
// [self addCameraRollButton:picker.view];
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)prepareCameraRoll {
isAction = 1;
[self dismissViewControllerAnimated:NO completion:nil];
}
- (IBAction)selectPhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.image = chosenImage;
isAction = 0;
[picker dismissViewControllerAnimated:YES completion:NULL];
[self performSegueWithIdentifier:@"toCameraView" sender:info];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
isAction = 2; // Cancel
[picker dismissViewControllerAnimated:YES completion:NULL];
}
# pragma mark - for the cameraOverlayView // suggestion from omz
- (UIView *)addCameraRollButton {
float startY = ([[UIScreen mainScreen] bounds].size.height == 568.0) ? 500.0 : 410.0;
UIButton *rollButton = [UIButton buttonWithType:UIButtonTypeCustom];
rollButton.frame = CGRectMake(230.0, startY, 60.0, 60.0);
rollButton.backgroundColor = [UIColor clearColor];
[rollButton setImage:self.lastTakenImage forState:UIControlStateNormal];
rollButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[rollButton addTarget:self action:@selector(prepareCameraRoll) forControlEvents:UIControlEventTouchUpInside];
return rollButton;
}
# pragma mark - CameraRoll function and presentation
- (void)addCameraRollButton:(UIView *)picker {
float startY = ([[UIScreen mainScreen] bounds].size.height == 568.0) ? 500.0 : 410.0;
UIButton *rollButton = [UIButton buttonWithType:UIButtonTypeCustom];
rollButton.frame = CGRectMake(230.0, startY, 60.0, 60.0);
rollButton.backgroundColor = [UIColor clearColor];
[rollButton setImage:self.lastTakenImage forState:UIControlStateNormal];
rollButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[rollButton addTarget:self action:@selector(prepareCameraRoll) forControlEvents:UIControlEventTouchUpInside];
[picker addSubview:rollButton];
}
-(void)cameraRoll {
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (nil != group) {
// be sure to filter the group so you only get photos
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset) {
ALAssetRepresentation *repr = [asset defaultRepresentation];
// UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
UIImage *img = [UIImage imageWithCGImage:[repr fullScreenImage]];
[self setLastTakenImage:img];
*stop = YES;
}
}];
}
*stop = NO;
} failureBlock:^(NSError *error) {
NSLog(@"error: %@", error);
}];
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CameraViewController *cvc = [segue destinationViewController];
cvc.image = self.image;
DLog(@"%@, cvcimage", cvc.image);
}
@end
【问题讨论】:
-
使用
UIImagePickerController的cameraOverlayView属性添加您的自定义按钮。 -
@omz 做到了,但没有解决问题。添加的按钮仍然位于“使用照片”按钮上方。更新了我的帖子,我是如何使用
cameraOverlayView属性的。 -
拍照时是否调用了您的 CameraViewController(基于您的头文件与 CamViewController 不同)?您从
didFinishPickingMediaWithInfo:方法中调用[self performSegueWithIdentifier:@"toCameraView" sender:info]方法,该方法在您拍照时调用。所以“toCameraView”是您需要实现“使用照片”按钮的地方。 -
@omz
CameraViewController是在拍照后第一次调用;严格来说,当按下“使用照片”按钮时,就会调用didFinishPickingMediaWithInfo:。相机和拍照功能仅在CamViewController可用。 “使用照片”按钮是默认按钮,它与UIImagePickerController一起提供。我想知道为什么叠加层在两个模态视图中可用(您拍摄照片的相机,以及您使用/重拍照片的预览)。人们如何在这里有不同的叠加层...... -
@jeremyw 见上面的评论。
标签: ios iphone objective-c camera uiimagepickercontroller