我正在c#/ xaml中为wp8开发一个杂志阅读器应用程序。我正在使用MuPDF库来呈现PDF页面。我正在以PDF格式下载每个页面。我使用MuPDF库将PDF渲染为图像。但是无法成功实现缩放功能。我在flipview中一次加载3页以获得分页效果.MuPDF返回Writeablebitmap作为PDF文件的输出,但是一旦创建了图像,它就会保留在内存中。即使我重新使用Writeablebitmap对象,ApplicationPeakmemory也总是很高。如何管理这个writeablebitmap以避免这种类型的记忆泄漏?请帮忙......
public async void CreatePDFDocument()
{
string imagePath = MagazineModel.Instance.BASEPATH + "\\" + MagazineModel.Instance.MAGAZINE_ID + "\\" + issue_id + "\\pdf" + "\\page" + pagenumber + ".pdf";
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
IStorageFile storageFile = await applicationFolder.GetFileAsync(imagePath);
using (var stream = await storageFile.OpenReadAsync())
{
IBuffer readBuffer;
using (IInputStream inputStreamAt = stream.GetInputStreamAt(0))
using (var dataReader = new DataReader(inputStreamAt))
{
uint bufferSize = await dataReader.LoadAsync((uint)stream.Size);
readBuffer = dataReader.ReadBuffer(bufferSize);
}
document = await Task.Factory.StartNew(() => Document.Create(readBuffer, DocumentType.PDF, (int)Windows.Graphics.Display.DisplayProperties.LogicalDpi));
if (document.NeedsPassword)
{
document.AuthenticatePassword("Mag#*%12");
}
}
PrepareBitmap(0);
image = await StartDraw(0, _bitmap,new Point());
}
private async Task<WriteableBitmap> StartDraw(int pageNumber, WriteableBitmap writeableBitmap,Point position)
{
await Task.Factory.StartNew(() =>
document.DrawPage(
pageNumber,
writeableBitmap.Pixels,
position.X,
position.Y,
writeableBitmap.PixelWidth,
writeableBitmap.PixelHeight,
false));
return writeableBitmap;
}
// Xaml code which binds WriteableBitmap to Flipview
<toolkit:FlipView ItemsSource="{Binding PageList}" x:Name="flipview" IsHitTestVisible="{Binding IsHit}"
SelectionChanged="flipview_SelectionChanged" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
>
<toolkit:FlipView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding image}"
RenderTransformOrigin="0,0" CacheMode="BitmapCache" HorizontalAlignment="Stretch"
SizeChanged="MapImage_SizeChanged">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener PinchStarted="GestureListener_PinchStarted"
PinchDelta="GestureListener_PinchDelta"
PinchCompleted="GestureListener_PinchCompleted"
DragStarted="GestureListener_DragStarted"
DragDelta="GestureListener_DragDelta"
DoubleTap="GestureListener_DoubleTap"
/>
</toolkit:GestureService.GestureListener>
<Image.RenderTransform>
<CompositeTransform
ScaleX="1" ScaleY="1"
TranslateX="0" TranslateY="0"/>
</Image.RenderTransform>
</Image>
</DataTemplate>
</toolkit:FlipView.ItemTemplate>
</toolkit:FlipView>