Iphone 如何保存拍摄的照片
我想保存我在应用程序中拍摄的照片。我想保存图片,这样即使我关闭应用程序,图片仍会保存。这是我迄今为止编写的代码Iphone 如何保存拍摄的照片,iphone,ios,save,photo,Iphone,Ios,Save,Photo,我想保存我在应用程序中拍摄的照片。我想保存图片,这样即使我关闭应用程序,图片仍会保存。这是我迄今为止编写的代码 -(IBAction)TakePhoto { picker = [[UIImagePickerController alloc]init]; picker.delegate = self; [picker setSourceType:UIImagePickerControllerSourceTypeCamera]; [self presentViewCo
-(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变量
您可能需要使用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];
}
在同一类的视图中写下:
//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 {
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];
}
在同一类的视图中写下:
//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];
}
//调用此方法从照相机捕获图片
- (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];
}
是否要将其保存在用户的相机卷中?还是在你自己的应用程序中?前者只是
UIImageWriteToSavedPhotosAlbum
C函数。是否要将其保存在用户的相机卷中?还是在你自己的应用程序中?前者只是UIImageWriteToSavedPhotosAlbum
C函数。