我正在用 xcode 中的 Cocoa 编写一个 iPhone 应用程序。我找不到任何教程或示例代码来展示如何使用内置相机拍照。我该怎么做呢?我在哪里可以找到好的信息?
谢谢!
我正在用 xcode 中的 Cocoa 编写一个 iPhone 应用程序。我找不到任何教程或示例代码来展示如何使用内置相机拍照。我该怎么做呢?我在哪里可以找到好的信息?
谢谢!
只需将以下代码复制并粘贴到您的项目中即可获得完全实现的功能。
其中takePhoto 和chooseFromLibrary 是我自己的方法名称,将在按钮触摸时调用。
确保将相应按钮的插座引用到这些方法。
-(IBAction)takePhoto :(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
-(IBAction)chooseFromLibrary:(id)sender
{
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[myImageView setImage:image]; // "myImageView" name of any UIImageView.
}
使用UIImagePickerController
. 这里有一个很好的教程。
http://www.zimbio.com/iPhone/articles/1109/Picking+Images+iPhone+SDK+UIImagePickerController
您应该将源类型设置为UIImagePickerControllerSourceTypeCamera
或UIImagePickerControllerSourceTypePhotoLibrary
。请注意,这两种类型会导致屏幕上的显示非常不同。您应该仔细测试两者。特别是,如果您UIImagePickerController
在 a 内部嵌套UINavigationController
,如果您不小心,最终可能会出现多个导航栏和其他奇怪的效果。
另请参阅此线程
该UIImagePickerController
课程可让您拍照或从照片库中选择它们。将源类型指定为UIImagePickerControllerSourceTypeCamera
。
另请参阅之前提出的这个问题: Access the camera with iPhone SDK
@WQS 发布的答案工作正常,但包含一些 iOS 6 已弃用的方法。以下是 iOS 6 及更高版本的更新答案:
-(void)takePhoto
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)chooseFromLibrary
{
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];
}
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[myImageView setImage:image]; // "myImageView" name of any UImageView.
}
不要忘记将其添加到您的view controller.h
:
@interface myVC<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
这是我用来为我的应用拍照的代码
- (IBAction)takephoto:(id)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[imageview setImage:img];
[self dismissViewControllerAnimated:YES completion:NULL];
}
如果你想重拍只需简单的添加这个功能
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}