使用Bitmap时,Xamarin Forms Android中会发生内存泄漏

问题描述 投票:0回答:1

在Xamarin Forms Android中使用位图时,我在加载高于通常大小的图像时出现内存不足错误。

void getImage() {
    -------     
    imageView.setImageBitmap(Bitmap.createScaledBitmap(getBitmapFromViewUpdated(_view), (int) width, (int) height, true));
    tempView = imageView;
    --------
}
public static Bitmap getBitmapFromView(final View view) {
    view.setLayoutParams(new LayoutParams(view.getWidth(),
            view.getHeight()));
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    final Bitmap bitmap_ = Bitmap
            .createBitmap(view.getMeasuredWidth(),
                    view.getMeasuredHeight(),
                    Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap_);
    view.draw(c);

    return bitmap_;
}

Bitmap getBitmapFromViewUpdated(final View view) {
    if (view.getWidth() == 0 || view.getHeight() == 0)
        view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    else
        view.setLayoutParams(new LayoutParams(view.getWidth(),
                view.getHeight()));
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, viewWidth, viewHeight);
    final Bitmap bitmap_;
    if(viewWidth>0 && viewHeight>0) {
        bitmap_ = Bitmap
                .createBitmap(viewWidth,
                        viewHeight, Bitmap.Config.ARGB_8888);
    }
    else
    {
       bitmap_= Bitmap
                .createBitmap(view.getMeasuredWidth(),
                        view.getMeasuredHeight(),
                        Bitmap.Config.ARGB_8888);
    }
    Canvas c = new Canvas(bitmap_);
    view.draw(c);

    return bitmap_;
}

我试过android:largeHeap=true,可以在使用时实现结果,但我需要知道如何修复位图大小调整。

memory-leaks bitmap xamarin.forms xamarin.android out-of-memory
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.