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

时间:2014-07-24 09:53:25

标签: ios xamarin.ios xamarin

我使用此代码在我的文档目录中保存了一张图片

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);

}

然后我通过此代码获取我的图像

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

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

        UIImage currentImage = UIImage.FromFile (jpgFilename);

现在我想从文档目录中删除此图像 我试试这段代码

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);

如何设法删除此图片

谢谢

1 个答案:

答案 0 :(得分:1)

答案比那更简单

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