美文网首页ios
打开相册

打开相册

作者: 大强哥 | 来源:发表于2015-03-27 13:27 被阅读128次

/**

*  打开相册

*/

- (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];

}

相关文章

网友评论

  • Dottie22:请问为什么要保存图片到沙盒中啊?还有如果我要拍照,一方面让拍的照片在我的程序中用,另一方面保存图片到我的系统相册,这个怎么弄,请教了:smile:
    Dottie22:哦哦,刚查到了,调用一个API就可以了,谢谢了:yum:
    大强哥:@touchldz 程序中用,直接在拍照的回调里拿到图片然后使用就行了,保存相册的话,你百度下,都有的

本文标题:打开相册

本文链接:https://www.haomeiwen.com/subject/aslnxttx.html