Domanda

I have written code to capture image from camera and i want to save that image in document directory. I have done the code but i think the image is not storing document directory. Because when i retrieve the images the camera images is not in documents directory,my code:

- (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];
È stato utile?

Soluzione

// While fetching NSDocumentDirectory Path it will give in the array format. So instead of giving NSString you need to give NSArray.. check below code

// Try this

NSArray * aDocumentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [aDocumentsDirectory objectAtIndex:0];
NSString *finaldocPath = [docPath stringByAppendingPathComponent:fileName]; 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top