-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage*image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data= UIImageJPEGRepresentation(image, 1.0);
}
else
{
data= UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString* DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager*fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPathwithIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPathstringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
// NSString* fileName =[@"kangli" stringByAppendingString:@"1" ];
filePath= [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
NSLog(@"图片的路径是:%@", filePath);
//关闭相册界面
[picker dismissModalViewControllerAnimated:YES];
//创建一个选择后图片的小图标放在下方
//类似微薄选择图后的效果
UIImageView*smallimage = [[UIImageView alloc] initWithFrame:CGRectMake(93, 85, 114, 104)];
smallimage.image= image;
//加在视图中
[self.view addSubview:smallimage];
// [saveButton setAlpha:0.0f];
}
}