我正在尝试在视图控制器中创建自定义ImagePicker(相机)。
这里是代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Instantiate the UIImagePickerController instance
self.picker = [[UIImagePickerController alloc] init];
// Configure the UIImagePickerController instance
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.picker.showsCameraControls = NO;
// Make us the delegate for the UIImagePickerController
self.picker.delegate = self;
// Set the frames to be full screen
CGRect screenFrame = [[UIScreen mainScreen] bounds];
self.view.frame = screenFrame;
self.picker.view.frame = screenFrame;
// Set this VC's view as the overlay view for the UIImagePickerController
self.picker.cameraOverlayView = self.view;
}
return self;
}
我所拥有的是一个带有我创建的按钮的黑色页面,仅此而已。当我按下拍照按钮时,它会拍摄一张真实的照片。 所以我的问题是UIImagePicker没有在我的视图中显示摄像头捕获。
任何人都可以帮助我。 提前谢谢。
答案 0 :(得分:0)
确保使用正确的委托方法在拍摄后显示图像。像这样:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *myPhotoData = [[NSData alloc] init];
myPhotoData = UIImageJPEGRepresentation(image, 0.5); // compressionQuality between 0.0 and 1.0 higher being less compression
myPhotoOutlet.image = [UIImage imageWithData:myPhotoData]; // myPhotoOutlet is an UIImageView outlet on your screen
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 1 :(得分:0)
您必须在.h文件中添加委托声明:
@interface YourViewcontroller : UIViewController <UIImagePickerControllerDelegate>
您必须在.m文件中实现委托方法才能获取图片:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *newImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// and then you affect your image in your image view
UIImageView *yourImageView= [[UIImageView alloc] initWithFrame:CGRectMake(0,0,300,300)];
yourImageView.image=newImage;
}
我希望能帮到你!