简体   繁体   English

从文档目录ios Xamarin中删除图像

[英]Delete Image from documents Directory ios Xamarin

I save a picture in my documents Directory with this code 我用此代码将图片保存在我的文档目录中

var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;

var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary;

// This bit of code saves to the application's Documents directory, doesn't save metadata

NSData imgData = photo.AsJPEG();

NSError err = null;

if (imgData.Save(jpgFilename, false, out err))

{

        Console.WriteLine("saved as " + jpgFilename);
} else {

        Console.WriteLine("NOT saved as" + jpgFilename + " because" + err.LocalizedDescription);

}

Then I get my image to see it with this code 然后我得到我的图像,用此代码可以看到它

var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg");

        UIImage currentImage = UIImage.FromFile (jpgFilename);

And now I want to delete this image from my documents directory I try this code 现在我想从我的文档目录中删除该图像,我尝试使用此代码

var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg");

NSFileManager nsf = new NSFileManager ();

NSError error = null;

nsf.Remove (jpgFilename, out error);

How can I manage to delete this image 我如何设法删除该图像

Thank you 谢谢

The answer is more simple than that 答案比那更简单

public void deleteImageInDirectory()
    {
        var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg");
        System.IO.File.Delete (jpgFilename);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM