照相、打开相册、保存图片到相册

一、保存图片到相册

UIImage*image = [UIImageimageNamed:[NSStringstringWithFormat:@"00%d.png", i]];

UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);

二、照相、打开相册

- (void)openCamera

{

[self    openImagePickerController:UIImagePickerControllerSourceTypeCamera];

}

- (void)openAlbum

{

//如果想自己写一个图片选择控制器,得利用AssetsLibrary.framework,利用这个框架可以获得手机上的所有相册图片

//UIImagePickerControllerSourceTypePhotoLibrary >UIImagePickerControllerSourceTypeSavedPhotosAlbum

[self  openImagePickerController:UIImagePickerControllerSourceTypePhotoLibrary];

}

- (void)openImagePickerController:(UIImagePickerControllerSourceType)type

{

if(![UIImagePickerController  isSourceTypeAvailable:type])  return;

UIImagePickerController* ipc = [[UIImagePickerControlleralloc]init];

ipc.sourceType= type;

ipc.delegate=self;

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

}

还要实现UIImagePickerControllerDelegate的方法

#pragma  mark - UIImagePickerControllerDelegate

/**

*从UIImagePickerController选择完图片后就调用(拍照完毕或者选择相册图片完毕)

*/

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info

{

[picker  dismissViewControllerAnimated:YES  completion:nil];

//info中就包含了选择的图片

UIImage*image = info[UIImagePickerControllerOriginalImage];

//添加图片到photosView中

[self.photosView  addPhoto:image];


}

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker

{


}

你可能感兴趣的:(照相、打开相册、保存图片到相册)