IOS调用相机相册

时间:2024-01-19 08:22:20

======================================================================================

#import "SendViewController.h"  //只能打开,没有加载图片的代码,老代码,供参考

#import <MobileCoreServices/UTCoreTypes.h>

@interface SendViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

-(IBAction)selectDescPic:(id)sender;

@end

@implementation SendViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

}

return self;

}

- (void)viewDidLoad {

[super viewDidLoad];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

-(IBAction)selectDescPic:(id)sender {

UIActionSheet *actionSheet = [[UIActionSheet alloc]

initWithTitle:nil

delegate:self

cancelButtonTitle:@"取消"

destructiveButtonTitle:nil

otherButtonTitles:@"拍照", @"从手机相册选择",nil];

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

[actionSheet showInView:self.view];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == 0) {

UIImagePickerController *camera = [[UIImagePickerController alloc] init];

camera.delegate = self;

camera.allowsEditing = NO;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

camera.sourceType = UIImagePickerControllerSourceTypeCamera;

//此处设置只能使用相机,禁止使用视频功能

camera.mediaTypes = [[NSArray alloc]initWithObjects:(NSString *)kUTTypeImage,nil];

} else {

NSLog(@"相机功能不可用");

return;

}

[self presentViewController:camera animated:YES completion:nil];

} else if (buttonIndex == 1) {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.allowsEditing = NO;

//从相册列表选取

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

//此处设置只能使用相机,禁止使用视频功能

picker.mediaTypes = [[NSArray alloc]initWithObjects:(NSString *)kUTTypeImage,nil];

}

[self presentViewController:picker animated:YES completion:nil];

} else if(buttonIndex == 2) {

//取消

}

}

@end

=========================================

将图片保存到相册  亲试可用

  - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 200, 300)];
_imageView.image = [UIImage imageNamed:@"hmt.jpg"];
_imageView.userInteractionEnabled = YES;
[self.view addSubview:_imageView]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[tapGesture addTarget:self action:@selector(tapSaveImageToIphone)];
[self.imageView addGestureRecognizer:tapGesture]; } - (void)tapSaveImageToIphone{ /**
* 将图片保存到iPhone本地相册
* UIImage *image 图片对象
* id completionTarget 响应方法对象
* SEL completionSelector 方法
* void *contextInfo
*/
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(voidvoid *)contextInfo{ if (error == nil) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil nil];
[alert show]; }else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil nil];
[alert show];
} }