您的位置:首页 > 移动开发 > IOS开发

IOS调用相机,保存到沙盒

2013-12-13 15:48 381 查看
- (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 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 照相机 压缩