簡體   English   中英

如何從圖庫加載照片並將其存儲到應用程序項目中?

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

我正在開發一個與我在此處找到的示例代碼幾乎相同的應用程序。 但是我想做的方法與示例代碼不同。
鏈接: 照片位置

第一個屏幕將具有ImageView和2個按鈕(選擇照片,確認)。 輕按“選擇照片”按鈕后,它將導航到另一個屏幕,該屏幕從我的iPad的照片庫中檢索照片。 選擇照片后,它將退出當前屏幕,並返回到在ImageView上顯示所選照片的​​第一個屏幕。

點擊“確認”按鈕后,照片將存儲到我的應用程序的項目中(例如/resources/images/photo.jpg)。

我可以知道該怎么做嗎?

這將帶您到圖庫,然后可以選擇圖像。

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

這將幫助您選擇圖像

- (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;
}

然后您可以將該圖像存儲到文檔目錄中...

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];

要自己點擊圖片,請使用此

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

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

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

這個委托方法將返回您從相冊中選擇的圖像

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

 - (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]; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM