简体   繁体   English

如何从相机拍照并通过编程方式保存在相册中?

[英]How to take picture from Camera & saved in Photo Gallery by programmatically?

In iPhone Apps, I want to take pictures from Camera & save into Photo Gallery by programmatically. 在iPhone应用程序中,我想从相机拍摄照片并通过编程方式保存到照片库。 Is it possible in iPhone Simulator? 在iPhone模拟器中可以吗? so please tell me any link or materials you have. 所以请告诉我你的任何链接或材料。

Thanks in advance 提前致谢

You would use uiimagepickercontroller for this purpose. 您可以使用uiimagepickercontroller来实现此目的。 First of all, import the MobileCoreServices.framework. 首先,导入MobileCoreServices.framework。 Then pull up the camera: 然后拉起相机:

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSArray *media = [UIImagePickerController
                          availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];

        if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            //picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            [picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];

            picker.delegate = self;
            [self presentModalViewController:picker animated:YES];
            //[picker release];

        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsupported!"
                                                            message:@"Camera does not support photo capturing."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unavailable!"
                                                        message:@"This device does not have a camera."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

Then, save the photo by implementing the uiimagepickercontrollerdelegate and a save function: 然后,通过实现uiimagepickercontrollerdelegate和保存功能来保存照片:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Media Info: %@", info);
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        //Save Photo to library only if it wasnt already saved i.e. its just been taken
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }


    }

    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alert;
    //NSLog(@"Image:%@", image);
    if (error) {
        alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                           message:[error localizedDescription]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
}

More info on image:didFinishSaving.... can be found here 有关图像的更多信息:didFinishSaving ....可以在这里找到

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html

Also have a look at this post 另外看看这篇文章

https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more

在模拟器中是不可能的。你必须使用该设备。

I do hope you realize that the simulator does not have a camera..... So I guess thats not possible. 我希望你意识到模拟器没有相机.....所以我猜这不可能。 You can definitely use a device.... You can make use of UIImagePickerController for capturing images. 你绝对可以使用设备......你可以利用UIImagePickerController来捕获图像。

There are 3 prerequesites needed to take a picture programmatically: 以编程方式拍摄照片需要3个先决条件:

  1. The device needs to have a camera check by 设备需要通过相机检查

      if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
  2. Camera controls need to be hidden (picker is a UIImagePickerController) 需要隐藏相机控件(选择器是UIImagePickerController)

     picker.showsCameraControls = NO; 
  3. use the takePicture from UIImagePickerController 使用UIImagePickerController中的takePicture

    [picker takePicture]; [picker takePicture];

Side note because it did bug me for a few minutes, the takePicture method also dismisses the view. 旁注,因为它确实让我误解了几分钟,takePicture方法也驳回了视图。

Also posted a more complete answer here Other Stack answer 另外在这里贴出了更完整的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式从画廊发送照片? - How to send photo from gallery programmatically? 如何使用设备相机在iphone App中以编程方式拍摄照片? - How to take Picture Programmatically in iphone App using device camera? 如何为每张图片命名,然后将其保存到iPhone的照片库中 - How to give name for each and every picture before it saved in photo gallery in iphone 如何从相机胶卷和照片库中选择多个视频? - How to select multiple video from camera roll and photo Gallery? Objective-C无需相机界面即可拍照的简单方法。 只需从相机获取图片并保存到文件 - Objective-C Simple way to take a photo without a camera interface. Just get a picture from camera and save to a file 如何使用iPhone应用程序从相机拍照 - how to take picture from camera using iphone app 当相机前面有一些动作时,是否可以通过编程方式从我的iphone拍照? - Is it possible to take picture from my iphone programmatically when there is some movement in front of camera? iOS从相机拍摄照片问题 - iOS Take photo from camera problem iPhone 相机 - 从库中选择或拍照 - iPhone Camera - Choose from Library or Take Photo 如何从iPhone的照片库中的私人文档目录文件夹中选择多张图片 - How to select multiple picture from private document directory folder not from photo gallery in iphone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM