簡體   English   中英

如何保存拍攝的照片

[英]How to save taken photo

我想保存在應用程序中拍攝的照片。 我想保存圖片,以便即使我關閉該應用程序也可以保存圖片。 這是我到目前為止編寫的代碼

-(IBAction)TakePhoto {
    picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker animated:YES completion:nil];
}

- (IBAction)ChooseExisting {
    picker2 = [[UIImagePickerController alloc]init];
    picker2.delegate = self;
    [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:picker2 animated:YES completion:nil];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageview setImage:image];
    [self dismissViewControllerAnimated:YES completion:nil];

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

我應如何使該應用程序保存拍攝的照片並顯示它。

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

UIImage * imageToSave = [UIImage imageNamed:@"Icon.png"];
NSData * binaryImageData = UIImagePNGRepresentation(imageToSave);

[binaryImageData writeToFile:[basePath stringByAppendingPathComponent:@"myfile.png"] atomically:YES];

從這里復制

您可能需要使用imageToSave變量編輯該行,並將其更改為從相機獲取的UIImage變量。

嘗試以下方法:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageview setImage:image];

    //Save Your Image ======
    UIImage *yourImage  = imageView.image;//imageView.image;
    NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"myImageFile.png"];
    [UIImagePNGRepresentation(yourImage) writeToFile:filePath atomically:YES];



    [self dismissViewControllerAnimated:YES completion:nil];

}

在同一個類的viewDidLoad中編寫以下代碼:

 //For Get your Image from Documents Dir
    NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"myImageFile.png"];

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
//if file already saved then set it on your imageView
        UIImage *getYourImage = [UIImage imageWithContentsOfFile:filePath];
        imageView.image = getYourImage;
    }
    else
    {
//otherwise set a Dummy Image on your ImageView or nil
        imageView.image = nil; //[UIImage imageNamed:@"dummyImage.png"];

    }

//調用此方法從相機捕獲圖片

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    //Get image
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //Display in ImageView object (if you want to display it
    [imageView setImage:image];
        [ButtonName setImage:image forState:UIControlStateNormal];
    //Take image picker off the screen (required)
    [self dismissModalViewControllerAnimated:YES];
}

//保存圖片:

- (NSString *)saveImage:(UIImage*)image:(NSString*)imageName 
{    
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.    
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
    NSLog(@"FULL PATH %@",fullPath);
    NSLog(@"image saved");
    return fullPath;
}

//刪除圖片

- (void)removeImage:(NSString*)fileName 
{   

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];

    [fileManager removeItemAtPath: fullPath error:NULL];

    NSLog(@"image removed");    
}

//從圖庫加載圖像

- (UIImage*)loadImage:(NSString*)imageName
 {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]];

    NSLog(@"Loading Image Path : %@",fullPath);

    return [UIImage imageWithContentsOfFile:fullPath];

}

暫無
暫無

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

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