照片库和照相机

来源:互联网 发布:根域名 cname www 编辑:程序博客网 时间:2024/05/01 14:30
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //获取Documents文件夹目录    
  5.     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
  6.     NSString *documentPath = [path objectAtIndex:0];    
  7.     //指定新建文件夹路径    
  8.     NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"];    
  9.     //创建ImageFile文件夹    
  10.     [[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];    
  11.     //保存图片的路径   
  12.     self.imagePath = [imageDocPath stringByAppendingPathComponent:@"image.png"];  
  13.      
  14. }  
  15.   
  16. -(void)viewWillAppear:(BOOL)animated{  
  17.     [super viewWillAppear:YES];  
  18.     //根据图片路径载入图片  
  19.     UIImage *image=[UIImage imageWithContentsOfFile:self.imagePath];  
  20.     if (image == nil) {  
  21.         //显示默认  
  22.         [changeImg setBackgroundImage:[UIImage imageNamed:@"user_photo@2x.png"] forState:UIControlStateNormal];  
  23.     }else {  
  24.         //显示保存过的  
  25.         [changeImg setBackgroundImage:image forState:UIControlStateNormal];  
  26.     }  
  27. }  
  28.   
  29. - (void)dealloc {  
  30.     [imagePath release];  
  31.     [changeImg release];  
  32.     [super dealloc];  
  33. }  
  34. - (IBAction)changeImage:(id)sender {  
  35.     UIActionSheet *myActionSheet = [[UIActionSheet alloc]                      
  36.                                     initWithTitle:nil  
  37.                                     delegate:self  
  38.                                     cancelButtonTitle:@"取消"  
  39.                                     destructiveButtonTitle:nil  
  40.                                     otherButtonTitles: @"从相册选择", @"拍照",nil];   
  41.     [myActionSheet showInView:self.view];  
  42.     [myActionSheet release];   
  43. }  
  44. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  45.     switch (buttonIndex) {  
  46.         case 0:  
  47.             //从相册选择  
  48.             [self LocalPhoto];  
  49.             break;  
  50.         case 1:  
  51.             //拍照  
  52.             [self takePhoto];  
  53.             break;  
  54.         default:  
  55.             break;  
  56.     }  
  57. }  
  58. //从相册选择  
  59. -(void)LocalPhoto{  
  60.     UIImagePickerController *picker = [[UIImagePickerController alloc] init];    
  61.     //资源类型为图片库  
  62.     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  63.     picker.delegate = self;  
  64.     //设置选择后的图片可被编辑  
  65.     picker.allowsEditing = YES;  
  66.     [self presentModalViewController:picker animated:YES];  
  67.     [picker release];  
  68. }  
  69.   
  70. //拍照  
  71. -(void)takePhoto{  
  72.     //资源类型为照相机  
  73.     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;  
  74.     //判断是否有相机  
  75.     if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){  
  76.         UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
  77.         picker.delegate = self;  
  78.         //设置拍照后的图片可被编辑  
  79.         picker.allowsEditing = YES;  
  80.         //资源类型为照相机  
  81.         picker.sourceType = sourceType;  
  82.         [self presentModalViewController:picker animated:YES];  
  83.         [picker release];  
  84.     }else {  
  85.         NSLog(@"该设备无摄像头");  
  86.     }  
  87. }  
  88. #pragma Delegate method UIImagePickerControllerDelegate    
  89. //图像选取器的委托方法,选完图片后回调该方法    
  90. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{  
  91.       
  92.     //当图片不为空时显示图片并保存图片  
  93.     if (image != nil) {    
  94.         //图片显示在界面上  
  95.         [changeImg setBackgroundImage:image forState:UIControlStateNormal];  
  96.           
  97.         //以下是保存文件到沙盒路径下  
  98.         //把图片转成NSData类型的数据来保存文件  
  99.         NSData *data;  
  100.         //判断图片是不是png格式的文件  
  101.         if (UIImagePNGRepresentation(image)) {  
  102.             //返回为png图像。  
  103.             data = UIImagePNGRepresentation(image);  
  104.         }else {  
  105.             //返回为JPEG图像。  
  106.             data = UIImageJPEGRepresentation(image, 1.0);  
  107.         }  
  108.         //保存  
  109.         [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];  
  110.           
  111.     }    
  112.     //关闭相册界面  
  113.     [picker dismissModalViewControllerAnimated:YES];  
  114. }  
原创粉丝点击