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

时间:2022-09-06 21:19:45

In iPhone Apps, I want to take pictures from Camera & save into Photo Gallery by programmatically. Is it possible in iPhone Simulator? so please tell me any link or materials you have.

在iPhone应用程序中,我想通过编程方式从相机拍摄照片并保存到照片库中。在iPhone模拟器中可以吗?所以请告诉我你的任何链接或材料。

Thanks in advance

提前致谢

4 个解决方案

#1


18  

You would use uiimagepickercontroller for this purpose. First of all, import the MobileCoreServices.framework. Then pull up the camera:

您可以使用uiimagepickercontroller来实现此目的。首先,导入MobileCoreServices.framework。然后拉起相机:

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

Also have a look at this post

另外看看这篇文章

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

#2


5  

it is not possible in simulator.You have to use the device.

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

#3


2  

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来捕获图像。

#4


0  

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];

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

另外在这里贴出了更完整的答案

#1


18  

You would use uiimagepickercontroller for this purpose. First of all, import the MobileCoreServices.framework. Then pull up the camera:

您可以使用uiimagepickercontroller来实现此目的。首先,导入MobileCoreServices.framework。然后拉起相机:

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

Also have a look at this post

另外看看这篇文章

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

#2


5  

it is not possible in simulator.You have to use the device.

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

#3


2  

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来捕获图像。

#4


0  

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];

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

另外在这里贴出了更完整的答案