IOS调用相机,保存到沙盒

来源:互联网 发布:新郎西服软件 编辑:程序博客网 时间:2024/06/08 06:49

- (IBAction)take_pictures_btn:(id)sender {       //设定sourceType为相机    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;       UIImagePickerController *picker = [[UIImagePickerController alloc] init];        picker.delegate = self;        picker.allowsEditing = NO; //设置为不可编辑        picker.sourceType = sourceType;        [self presentModalViewController:picker animated:YES];//进入照相界面}#pragma mark - image picker delegte- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{     [picker dismissViewControllerAnimated:YES completion:^{}];     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];    //压缩图片,格式为JPEG,压缩率为50%    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);        // 获取沙盒目录    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];        // 将图片写入文件    [imageData writeToFile:fullPath atomically:NO];            UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];        [self.imageView setImage:savedImage];}

在.h文件中要加上代理: UINavigationControllerDelegate、UIImagePickerControllerDelegate 

0 0
原创粉丝点击