我正在开发一款iOS应用。用户可以用相机拍照,这张照片应该显示并保存在我的应用程序中。即使应用程序重新启动。我有以下代码来拍照:
- (void)takePictureFromCamera {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
_newMedia = YES;
}
}
这种将图片保存到相机胶卷的方法:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
if (_newMedia) {
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
}
}
}
如何在我的应用程序中保存图片,如果应用程序重新启动,则直接显示网格中的早期拍摄图片或类似内容。
答案 0 :(得分:3)
帮助方法
-(NSURL *)getDocumentsPathURL
{
//document directory of app
return [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:nil];
}
您的UIImagePickerController
委托方法:
#define k_IMAGE_NAME @"DefaultImage.png"
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
//append filename to document directory url
NSURL *urlSave = [[self getDocumentsPathURL] URLByAppendingPathComponent:k_IMAGE_NAME];
//do something to previously taken image (in this case: delete it)
if ([[NSFileManager defaultManager] fileExistsAtPath:urlSave.path]) {
[[NSFileManager defaultManager] removeItemAtPath:urlSave.path error:nil];
}
//save image to document directory
[UIImagePNGRepresentation(image) writeToFile:urlSave.path atomically:YES];
//optional: save image to photo library
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
}
加载图片的方式(应用启动时或任何时候)
-(void)loadDefaultImage
{
NSURL *urlImage = [[self getDocumentsPathURL] URLByAppendingPathComponent:k_IMAGE_NAME];
if ([[NSFileManager defaultManager] fileExistsAtPath:urlImage.path]) {
NSData *dataFile = [[NSFileManager defaultManager] contentsAtPath:urlImage.path];
UIImage *image = [UIImage imageWithData:dataFile];
[imageView setImage:image];
}
else {
NSLog(@"Image not found");
}
}
答案 1 :(得分:0)
回答你的第一个问题:
[UIImagePNGRepresentation(your_image) writeToFile:filePath atomically:YES];
您的代码变为:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
}
}
答案 2 :(得分:0)
您可以将图像保存在apps文档目录中
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
if(image)
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [path objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"anyName.png"];
[image writeFileToPath : filePath];
}
}
}
For retrieving the file you can use
NSData *imgData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:imgData];