如何从相机拍摄照片通过编程保存在照片库中? [英] How to take picture from Camera & saved in Photo Gallery by programmatically?

查看:138
本文介绍了如何从相机拍摄照片通过编程保存在照片库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone应用程式中,我想从相机&通过编程保存到照片库。有可能在iPhone模拟器?



提前感谢

解决方案

您将使用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:@不支持!
message:@相机不支持拍照。
delegate:nil
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];
[alert release];
}

}
else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Unavailable!
message:@此设备没有摄像头。
delegate:nil
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];
[警报版本];然后,通过实现uiimagepickercontrollerdelegate和保存功能保存照片: p>

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

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

//只有当它已经保存时才将照片保存到库中,即它刚刚被采用
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
UIImageWriteToSavedPhotosAlbum(photoTaken,self,@ selector(image:didFinishSavingWithError:contextInfo :),nil);
}


}

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

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)错误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];
[警报版本];
}

}

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

关于图片的更多信息:didFinishSaving ....可以在这里找到

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



另请参阅此帖



UIImagePickerController,UIImage,Memory和更多!


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.

Thanks in advance

解决方案

You would use uiimagepickercontroller for this purpose. First of all, import the 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:

- (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

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

Also have a look at this post

UIImagePickerController, UIImage, Memory and More!

这篇关于如何从相机拍摄照片通过编程保存在照片库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆