簡體   English   中英

如何保存使用Camera捕獲的圖像並將其保存在另一個ViewController中?

[英]How to save the image captured using Camera and save in another ViewController?

如何保存使用Camera捕獲的圖像並將其保存在另一個ViewController中?

我正在制作照片庫應用程序。 但是我不知道如何保存捕獲的圖像並在另一個viewcontroller的集合視圖中顯示它。 目前我正在使用UIImagePickerController來訪問iphone相機和庫。有人可以幫我嗎。

這是我的代碼。

提前致謝

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (IBAction)takePhoto:(UIButton *)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];

}

- (IBAction)selectPhoto:(UIButton *)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];


}

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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}




- (void)viewDidLoad {
    [super viewDidLoad];
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Device has no camera"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];

        [myAlertView show];

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

嘗試這個

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  // Access the uncropped image from info dictionary
  UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

  // Save image
  UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

  [picker release];
}

您可以通過添加NSNotificationcenter來實現。

A類 - 從相機中選擇圖像的位置

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
     UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
     self.imageView.image = chosenImage;
     [picker dismissViewControllerAnimated:YES completion:NULL];
    [[NSNotificationCenter defaultCenter] postNotificationName: @"GotNewImage" object: chosenImage];
}

B類 -選擇后要傳遞圖像並顯示在集合視圖上的位置。 在Viewdidload中為此類添加Nsnotificationcenter

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GotNewImage:) name:@"GotNewImage" object:nil];

//獲取B類上的圖像

-(void)GotNewImage:(NSNotification*)notification
 {
     UIImage * newImage = (UIImage*) notification.object;
     //Now do whatever you want to do with your image...
     //Add in collection view and reload the collection view
 }

暫無
暫無

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

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