在Windows Phone 8的WriteableBitmap的内存泄漏 [英] WriteableBitmap memory leak in Windows Phone 8

查看:106
本文介绍了在Windows Phone 8的WriteableBitmap的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内存泄漏每当我创建一个 WriteableBitmap的的任何实例。我试过在计算器和其他论坛多次建议,但没有什么工作。我的测试应用程序的基本流程是这样的:




  1. 选择与图片 PhotoChooserTask

  2. 使用 PhotoResult 对象创建一个 WriteableBitmap的



这就是它。调零的变量和调用 GC.Collect的()只解决了问题的一部分。它使应用程序从直到应用程序崩溃分配内存,但即使对象已超出范围,总是有分配给他们,直到我选择一个新的图像存储器。我可以用XAML应用程序默认的Windows Phone的Direct3D重现。唯一的修改默认的项目如下:



MainPage.xaml.cs中



 公开的MainPage(){
的InitializeComponent();
_photoChooserTask =新PhotoChooserTask();
_photoChooserTask.Completed + =新的EventHandler< PhotoResult>(photoChooserTaskComplete);
}

私人无效ApplicationBarIconButton_Click(对象发件人,EventArgs五){
_photoChooserTask.Show();
}

私人无效photoChooserTaskComplete(对象发件人,PhotoResult E){
如果(e.TaskResult == TaskResult.OK){
的BitmapImage形象=新的BitmapImage( );
image.SetSource(e.ChosenPhoto);
WriteableBitmap的WBM =新的WriteableBitmap的(形象);
image.UriSource = NULL;
图像= NULL;
WBM = NULL;
GC.Collect的();
}
}



MainPage.xaml中



 <电话:PhoneApplicationPage.ApplicationBar> 
<外壳:应用程序任务栏ISVISIBLE =真IsMenuEnabled =真模式=默认不透明度=0.5>
<外壳:ApplicationBar.Buttons>
<外壳:ApplicationBarIconButton IconUri =/ junkUrl.png文本=专辑点击=ApplicationBarIconButton_Click/>
< /壳:ApplicationBar.Buttons>
< /壳:应用程序任务栏>
< /电话:PhoneApplicationPage.ApplicationBar>


解决方案

有关这一点,你必须存储里面的这个文件流IsolatedStorege。因此,使用IsolatedStorageFileStream创建一个文件流,然后将其保存,像这样...

 私人无效photoChooserTaskComplete(对象发件人,PhotoResult E) {
如果(e.TaskResult == TaskResult.OK){
SaveToIsolatedStorage(e.ChosenPhoto,你的文件名);
}



}

 公共无效SaveToIsolatedStorage(流的ImageStream,字符串文件名)
{

{
串imagename =文件名+.JPG;使用
(IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
如果(myIsolatedStorage.FileExists(imagename))
{
myIsolatedStorage.DeleteFile(imagename);
}
IsolatedStorageFileStream FILESTREAM = myIsolatedStorage.CreateFile(imagename);
WriteableBitmap的WB =新WriteableBitmap的(100,100);
wb.SetSource(的ImageStream);
wb.SaveJpeg(FILESTREAM,100,100,0,70);
fileStream.Close();
}
}

赶上(例外)
{
RadMessageBox.Show(的String.Empty,MessageBoxButtons.OK,而保存图像时出错 );
}

}

和阅读你可以得到从IsolatedStorage文件

 公共WriteableBitmap的ReadFromIsolatedStorage(字符串文件名)
{
WriteableBitmap的位图=新的WriteableBitmap的(100 ,100);

{使用
(IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
如果(myIsolatedStorage.FileExists(文件名))
{

使用(IsolatedStorageFileStream FILESTREAM = myIsolatedStorage.OpenFile(文件名,FileMode.Open,FileAccess.Read))
{
//解码的JPEG视频流。
位= PictureDecoder.DecodeJpeg(FILESTREAM,100,100);
}
}
}
}
赶上(例外)
{
RadMessageBox.Show(的String.Empty,MessageBoxButtons.OK错误Occcured同时读取图像);
}


返回位图;
}

这将解决您的内存泄漏问题,试试这个...


I'm having a memory leak whenever I create any instance of a WriteableBitmap. I've tried multiple suggestions on stackoverflow and other forums, but nothing is working. The basic flow of my test app is this:

  1. Select an image with the PhotoChooserTask
  2. Use the Stream from the PhotoResult object to create a WriteableBitmap.

That's it. Nulling the variables and calling GC.Collect() only solves part of the problem. It keeps the app from allocating memory until the app crashes, but even though the objects have gone out of scope, there is always memory allocated for them until I select a new image. I can reproduce it with the default Windows Phone Direct3D with XAML App. The only modifications to the default project are the following:

MainPage.xaml.cs

public MainPage() {
    InitializeComponent();
    _photoChooserTask = new PhotoChooserTask();
    _photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTaskComplete);
}

private void ApplicationBarIconButton_Click(object sender, EventArgs e) {
    _photoChooserTask.Show();
}

private void photoChooserTaskComplete(object sender, PhotoResult e) {
    if (e.TaskResult == TaskResult.OK) {
        BitmapImage image = new BitmapImage();
        image.SetSource(e.ChosenPhoto);
        WriteableBitmap wbm = new WriteableBitmap(image);
        image.UriSource = null;
        image = null;
        wbm = null;
        GC.Collect();
    }
}

MainPage.xaml

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Default" Opacity="0.5" >
        <shell:ApplicationBar.Buttons>
            <shell:ApplicationBarIconButton IconUri="/junkUrl.png" Text="albums" Click="ApplicationBarIconButton_Click" />
        </shell:ApplicationBar.Buttons>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

解决方案

For this you have to store this file stream inside the IsolatedStorege. So create a filestream using IsolatedStorageFileStream and then save it, like this...

private void photoChooserTaskComplete(object sender, PhotoResult e) {
if (e.TaskResult == TaskResult.OK) {
       SaveToIsolatedStorage(e.ChosenPhoto,"Your File Name");           
}

}

 public void SaveToIsolatedStorage(Stream imageStream, string fileName)
    {
        try
        {
            string imagename =  fileName + ".jpg";
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(imagename))
                {
                    myIsolatedStorage.DeleteFile(imagename);
                }
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(imagename);
                WriteableBitmap wb = new WriteableBitmap(100, 100);
                wb.SetSource(imageStream);
                wb.SaveJpeg(fileStream, 100, 100, 0, 70);
                fileStream.Close();
            }
        }

        catch (Exception)
        {
            RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error occured while saving Images");                               
        }

    }

And for reading you can get that file from IsolatedStorage

public WriteableBitmap ReadFromIsolatedStorage(string fileName)
    {
        WriteableBitmap bitmap = new WriteableBitmap(100, 100);
        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(fileName))
                {

                    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                    {
                        // Decode the JPEG stream.                            
                        bitmap = PictureDecoder.DecodeJpeg(fileStream, 100, 100);
                    }
                }
            }
        }
        catch (Exception)
        {
            RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error Occcured while reading image");                               
        }


        return bitmap;
    }

This will solved your memory leak problem, try this...

这篇关于在Windows Phone 8的WriteableBitmap的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆