【问题标题】:Camera image not saving in document directory相机图像未保存在文档目录中
【发布时间】:2013-07-29 04:32:36
【问题描述】:

我已经编写了从相机捕获图像的代码,我想将该图像保存在文档目录中。我已经完成了代码,但我认为图像没有存储文档目录。因为当我检索图像时,相机图像不在文档目录中,所以我的代码:

- (IBAction)takePhoto {

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

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

}


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

UIImage *cameraImage = info[UIImagePickerControllerEditedImage];
self.tempView.image = cameraImage;
        [_chosenImages addObject:_tempView.image];
[picker dismissViewControllerAnimated:YES completion:NULL];

}

id n = [array3 lastObject];
    NSLog(@"The id is=%@",n);


    NSError *error;
    NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dataPath = [aDocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",n]];
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

    NSInteger anIndex = 0;
    for (UIImage *anImage in _chosenImages) {
        NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
        NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", dataPath, anImageName];
        NSLog(@"the path is=%@",anImagePath);

        NSData *anImageData = UIImagePNGRepresentation(anImage);
        [anImageData writeToFile:anImagePath atomically:YES];

【问题讨论】:

  • 这个语句给你什么,NSLog(@"the path is=%@",anImagePath);和 NSLog anImageData 检查数据是否为空
  • 我正在 iPhone 上测试我不知道..
  • 我认为问题不在于存储代码..而是获取代码..你能说明你是如何从文档目录中获取图像的吗??
  • 获取代码是正确的。因为照片库中的图像获取正确..

标签: ios camera nsdocumentdirectory


【解决方案1】:

// 在获取 NSDocumentDirectory 路径时,它将以数组格式给出。所以不是给 NSString 你需要给 NSArray.. 检查下面的代码

// 试试这个

NSArray * aDocumentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [aDocumentsDirectory objectAtIndex:0];
NSString *finaldocPath = [docPath stringByAppendingPathComponent:fileName]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    相关资源
    最近更新 更多