gpt4 book ai didi

ios - 仅当它们由相机拍摄而不是从照片库中选择时才将图像保存到照片库

转载 作者:行者123 更新时间:2023-12-01 19:02:41 26 4
gpt4 key购买 nike

我的应用程序中有一个照片选择器,它会询问用户是要拍照还是从照片库中选择一张。完成他们的选择后,我为他们选择的图像设置了一个 ImageView ,我想保存他们刚刚设置的图像,前提是它是从相机拍摄的,而不是他们已经拥有的照片的编辑版本。此处列出的最后一种方法是我希望保存照片的方法。

- (IBAction)startPicker:(id)sender {

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIActionSheet *picChoice = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Photo Library", nil];
[picChoice showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
} else {
UIActionSheet *picChoice = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo Library", nil];
[picChoice showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
}
}


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

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;

if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Photo Library"]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else {
return;
}
[self presentViewController:imagePicker animated:YES completion:^{

}];
}

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

UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:editedImage.CGImage orientation:(ALAssetOrientation)editedImage.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
{
NSLog(@"IMAGE SAVED TO PHOTO ALBUM");
[library assetForURL:assetURL resultBlock:^(ALAsset *asset )
{
NSLog(@"we have our ALAsset!");
}
failureBlock:^(NSError *error )
{
NSLog(@"Error loading asset");
}];
}];

最佳答案

只需检查 sourceType :

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// save to library
}

在您的 imagePickerController:didFinishPickingMediaWithInfo: 中执行此操作方法。

关于ios - 仅当它们由相机拍摄而不是从照片库中选择时才将图像保存到照片库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21818831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com