/**
*? 打開相冊(cè)
*/
- (IBAction)openPhotoLibiary:(UIButton *)sender
{
??? //打開相冊(cè)
??? UIImagePickerController *picker = [[UIImagePickerController alloc] init];
??? //資源類型為圖片庫
??? picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
??? picker.delegate = self;
??? //設(shè)置選擇后的圖片可被編輯
??? picker.allowsEditing = YES;
??? [self presentViewController:picker animated:YES completion:nil];
}
#pragma Delegate - 相冊(cè) UIImagePickerControllerDelegate
//圖像選取器的委托方法,選完圖片后回調(diào)該方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//當(dāng)圖片不為空時(shí)顯示圖片并保存圖片
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)閉相冊(cè)界面
[picker dismissModalViewControllerAnimated:YES];
}