也是看到简书里一哥们的,忘了是谁的了,只是他不会排版,我又给整理了一下!
// 打开相册
- (IBAction)openPhotoLibiary:(UIButton *)sender
{
//打开相册
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//资源类型为图片库
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
pragma Delegate - 相册 UIImagePickerControllerDelegate
//图像选取器的委托方法,选完图片后回调该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//当图片不为空时显示图片并保存图片
if (image != nil) {
//图片显示在界面上
[changeImg setBackgroundImage:image forState:UIControlStateNormal];
//以下是保存文件到沙盒路径下
//把图片转成NSData类型的数据来保存文件
NSData *data;
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data = UIImagePNGRepresentation(image);
}else {
//返回为JPEG图像。
data = UIImageJPEGRepresentation(image, 1.0);
}
//保存
// [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];
}
//关闭相册界面
[picker dismissModalViewControllerAnimated:YES];
}
网友评论