相机、相册

//调用相册

// 调用系统相册的类
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];

// 设置选取的照片是否可编辑
pickerController.allowsEditing = YES;
// 设置相册呈现的样式
pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// 选择完成图片或者点击取消按钮都是通过代理来操作我们所需要的逻辑过程
pickerController.delegate = self;
// 使用模态呈现相册
[self presentViewController:pickerController animated:YES completion:nil];


#pragma mark --- 拍完照或者相册选择照片后的代理方法
// 选择照片完成之后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *resultImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    //压缩并存储
    NSData *imgData = UIImageJPEGRepresentation(resultImage, 0.001);
    NSString *path_sandox = NSHomeDirectory();
    NSString *newPath = [path_sandox stringByAppendingPathComponent:@"/Documents/pic.png"];
    if ([imgData writeToFile:newPath atomically:YES]) {
        NSLog(@"写入成功");
    };
    NSLog(@"图片大小--->%lldK",[self fileSizeAtPath:newPath]);
}

//点击取消按钮所执行的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //这是捕获点击右上角cancel按钮所触发的事件,如果我们需要在点击cancel按钮的时候做一些其他逻辑操作。就需要实现该代理方法,如果不做任何逻辑操作,就可以不实现
    [self dismissViewControllerAnimated:YES completion:nil];
}

调用相机


你可能感兴趣的:(相机、相册)