ios 如何从相册加载照片并将其存储到应用程序项目中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7605845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:41:11  来源:igfitidea点击:

How to load photos from photo gallery and store it into application project?

iphoneobjective-ciosxcodeipad

提问by Lloydworth

I'm working on an application that is almost the same as the sample codes i found here. But the method i want to do is different from the sample codes.
Link: PhotoLocations

我正在开发一个与我在此处找到的示例代码几乎相同的应用程序。但是我想做的方法与示例代码不同。
链接:照片位置

The first screen will have an ImageView and 2 buttons (Choose Photo, Confirm). When the 'Choose Photo' button is tapped, it will navigate to another screen which retrieves photos from my iPad's photo gallery. When a photo is chosen, it will dismiss the current screen and return to the first screen displaying the chosen photo on the ImageView.

第一个屏幕将有一个 ImageView 和 2 个按钮(选择照片,确认)。当点击“选择照片”按钮时,它将导航到另一个屏幕,该屏幕从我的 iPad 的照片库中检索照片。选择照片时,它将忽略当前屏幕并返回显示ImageView上所选照片的​​第一个屏幕。

When the 'Confirm' button is tapped, the photo will be stored into my application's project (e.g. /resources/images/photo.jpg).

当“确认”按钮被点击时,照片将被存储到我的应用程序的项目中(例如 /resources/images/photo.jpg)。

May i know how can i do this?

我可以知道我该怎么做吗?

回答by Ankit Srivastava

This will take you to the image gallery and you can select the image.

这将带您进入图像库,您可以选择图像。

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];

this will help you select the image

这将帮助您选择图像

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
    // Dismiss the image selection, hide the picker and

    //show the image view with the picked image

    [picker dismissViewControllerAnimated:YES completion:nil];
    //UIImage *newImage = image;
}

And then you can store this image to the documents directory...

然后您可以将此图像存储到文档目录...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

For clicking the image yourself use this

要自己单击图像,请使用此

- (IBAction) takePhoto:(id) sender
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:imagePickerController animated:YES];
}

回答by BooleanBoy

UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init];
cardPicker.allowsEditing=YES;
cardPicker.delegate=self;
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:appDelegate.cardPicker animated:YES];
[cardPicker release];

And this delegate method will return the image u select from the album

这个委托方法将返回你从相册中选择的图像

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    noImage.image=img;
}

回答by Purushothaman

- (IBAction)UploadPhoto:(id)sender {
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please Select Your Option"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery",nil];
    
    [actionSheet showInView:self.view];
    
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    
    NSIndexPath *clickedButtonPath = [jobstable indexPathForCell:clickedCell];
    
    isSectionIndex = clickedButtonPath.section;
    isRowIndex     = clickedButtonPath.row;
    
}


-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
    
    NSLog(@"From didDismissWithButtonIndex - Selected Option: %@", [actionSheet buttonTitleAtIndex:buttonIndex]);
    
    NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex];
    
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) {
        
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            return;
        }
        else{
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:picker animated:YES completion:NULL];
            
        }
    }
    
    else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Gallery"]){
        
        
        UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
        pickerView.allowsEditing = YES;
        pickerView.delegate = self;
        [pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        [self presentViewController:pickerView animated:YES completion:nil];
        
    }
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex];
        
        UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];
        
        UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];
        
        tableIMAGE.image=orginalImage;
        
  answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@,",answersARRAY[indexPath.row],imageStris];

    
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
        
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        
        [picker dismissViewControllerAnimated:YES completion:NULL];
    
    }