iPhone: How do I get the file path of an image sav

2019-01-03 13:34发布

I'm saving a merged image to the iPhone photo library using:

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);

And getting the callback using:

- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo { NSLog(@"%@", [error localizedDescription]);
NSLog(@"info: %@", contextInfo);}

What I would like to get is the path for where the image has been saved, so I can add it to an array which will be used to call up a list of saved items elsewhere in the app.

When I load the image using the picker it displays the path info. However when I save a created image, I can't find where to pull the saved image path.

I have search about the web, but most examples stop at the callback with a nice message to say the image was saved successfully. I would just like to be able to know where it was saved.

I understand one method might be to start defining my own paths, but as the method does that for me, I was just hoping it could tell me where it saved to.

7条回答
爷、活的狠高调
2楼-- · 2019-01-03 14:34
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {

    RandomIndexnew = arc4random() % 3;
    if(RandomIndexnew == 0)
    {
        nameStr =[NSString stringWithFormat:@"jpg"];
        textFieldNormalFile_type.text =[NSString stringWithFormat:@"jpg"];
    }
    else if(RandomIndexnew = 1)
    {
        nameStr =[NSString stringWithFormat:@"gif"];
        textFieldNormalFile_type.text =[NSString stringWithFormat:@"GIF"];
    }
    else if(RandomIndexnew = 2)
    {
        nameStr =[NSString stringWithFormat:@"jpg"];
        textFieldNormalFile_type.text =[NSString stringWithFormat:@"JPG"];
    }

    RandomIndex = arc4random() % 20;
    NSString *nameStr1 =[NSString stringWithFormat:@"Image%i",RandomIndex];
    textFieldNormalFile_name.text =[NSString stringWithFormat:@"%@.%@",nameStr1,nameStr];

    newFilePath = [NSHomeDirectory() stringByAppendingPathComponent: textFieldNormalFile_name.text];
    imageData = UIImageJPEGRepresentation(img, 1.0);
    if (imageData != nil) {
        NSLog(@"HERE [%@]", newFilePath);
        [imageData writeToFile:newFilePath atomically:YES];
    }
    image.image =[UIImage imageNamed:newFilePath];
    NSLog(@"newFilePath:%@",newFilePath);
    path.text =[NSString stringWithFormat:newFilePath];
    NSLog(@"path.text :%@",path.text);
}
查看更多
登录 后发表回答