也是看到簡書里一哥們的,忘了是誰的了,只是他不會排版,我又給整理了一下!
// 打開相冊
- (IBAction)openPhotoLibiary:(UIButton *)sender
{
//打開相冊
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//資源類型為圖片庫
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//設(shè)置選擇后的圖片可被編輯
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
pragma Delegate - 相冊 UIImagePickerControllerDelegate
//圖像選取器的委托方法,選完圖片后回調(diào)該方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//當(dāng)圖片不為空時顯示圖片并保存圖片
if (image != nil) {
//圖片顯示在界面上
[changeImg setBackgroundImage:image forState:UIControlStateNormal];
//以下是保存文件到沙盒路徑下
//把圖片轉(zhuǎn)成NSData類型的數(shù)據(jù)來保存文件
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];
}
//關(guān)閉相冊界面
[picker dismissModalViewControllerAnimated:YES];
}