简体   繁体   English

如何保存拍摄的照片

[英]How to save taken photo

I want to save my picture that I took in the app. 我想保存在应用程序中拍摄的照片。 I want to save the picture so that even if I close the app the picture is still saved. 我想保存图片,以便即使我关闭该应用程序也可以保存图片。 this is my code that i have written until now 这是我到目前为止编写的代码

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

How should I make the app save the picture that I took and display it. 我应如何使该应用程序保存拍摄的照片并显示它。

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

Copied from here 从这里复制

You may need to edit the line with the imageToSave variable and change it to the UIImage variable, you got from the camera. 您可能需要使用imageToSave变量编辑该行,并将其更改为从相机获取的UIImage变量。

Try this In your method: 尝试以下方法:

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

}

And in your viewDidLoad of the same class write this: 在同一个类的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"];

    }

// Call this method to capture picture from camera //调用此方法从相机捕获图片

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

// to save an image : //保存图片:

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

// to remove an image //删除图片

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

// to load an image from gallery //从图库加载图像

- (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.

相关问题 如何将使用AVFoundation拍摄的照片保存到相册? - How to save photos taken using AVFoundation to Photo Album? 如何保存用UIImagePickerController相机拍摄的照片以稍后在应用程序中显示? - How can I save a photo taken with UIImagePickerController camera to be displayed later in the app? 通过电子邮件发送使用ImagePickerController拍摄的照片? - Emailing photo taken with ImagePickerController? iPhone SDK-如何在UINavigationController中显示用相机拍摄的照片? - iPhone SDK - How to display a photo taken with the camera inside a UINavigationController? iPhone如何在相机应用程序中获得照片拍摄效果? - iPhone How to get photo taken effect like in camera application? 如何检测照片库中的图像是由iPhone相机拍摄还是导入 - how to detect if image from photo library was taken by iPhone camera or imported 如何设置使用UIImagePickerController拍摄的照片进行查看/修改为自定义UIViewController? - How to set photo taken with UIImagePickerController to be viewed/modified into a custom UIViewController? 将照片保存到相册 - Save photo to photo album 如何保存带有标签的照片? - How do I save a photo with a label overlaid on the photo? 如何使用其名称保存和检索相册中的照片 - How to save and retrieve photos in photo album with their names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM