0

我正在尝试在 ipad 上拍照并将图像存储在图像视图中 - 但是我不断收到内存警告,然后程序崩溃......有没有办法通过保留图像来解决这些内存问题......

这是我的代码:

//UIImagePickerControllerDelegate

    class CameraDelegate : UIImagePickerControllerDelegate {

        private string meterId = string.Empty;
        private int ext = 0;
        SaveStringElement ele;
        PictureType type;
        NSObject image;
        UIImage picture;
        Section section;
        NSData data;
        Stream s;
        //UIImageView imageView;

        public CameraDelegate(string id, int count, SaveStringElement e)
        {
            type = PictureType.Machine_Meter;
            meterId = id;
            ext = count;
            ele = e;
        }

        public CameraDelegate()
        {
            type = PictureType.Check_MoneyOrder;
        }


        public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
        {
            switch (type) {
            case PictureType.Machine_Meter:
                try {
                    picker.DismissModalViewControllerAnimated (false);
                    sendToServer(info, meterId +"-"+ext);

                    if (ext == 1) 
                    {
                        MessageBox.Show ("Now picture number two...");
                        TakePicture (meterId, 2, ele, PictureType.Machine_Meter);
                    } 
                    else 
                    {
                        CleanUpScreen (ele);
                    }
                } 
                catch (Exception ex) {
                    var alert = new UIAlertView ("Error processing camera info...", ex.Message + "...try again", null, "OK", null);
                    alert.Show ();
                }
                break;
            case PictureType.Check_MoneyOrder:
                try {
                    picker.DismissModalViewControllerAnimated (false);
                    sendToServer(info, type.ToString());
                } 
                catch {
                }

                break;
            }

            picker.Dispose();
            picker = null;
        }

        void sendToServer(NSDictionary info, string name)
        {
            image = info [NSObject.FromObject ("UIImagePickerControllerOriginalImage")];
            picture = image as UIImage;
            picture.Scale(new SizeF(300, 300));
            imageView = new UIImageView (){Image = picture, ContentMode = UIViewContentMode.ScaleAspectFit, ClipsToBounds= false, Frame = new RectangleF(0,0, 300, 300) };

            if(type == PictureType.Machine_Meter)
            {
                section = new Section () { 
                    HeaderView = imageView
                };

                UpdatePicture (section);
            }

            data = picture.AsJPEG ();
            s = data.AsStream ();

            string Error;
            URMServer.CreateUrmServer ().SaveUploadedPictures (s, currentLocation.ID, type.ToString(), name, out Error);

            image.Dispose();
            image = null;

            picture.Dispose();
            picture = null;

            data.Dispose();
            data = null;

            s.Dispose();
            s = null;

            /*imageView.Image.Dispose();
            imageView.Image = null;
            imageView.Dispose();
            imageView = null;*/

            GC.Collect();
        }
    }

//拍照功能

void TakePictures (string value, int counter, SaveStringElement e, PictureType pt)
        {
            picker = new UIImagePickerController();
            picker.SourceType = UIImagePickerControllerSourceType.Camera;
            picker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.Camera);
            picker.ShowsCameraControls = true;

            switch(pt)
            {
                case PictureType.Machine_Meter:
                    picker.Delegate = new CameraDelegate (value, counter, e);
                    break;
                case PictureType.Check_MoneyOrder:
                    picker.Delegate = new CameraDelegate ();
                    break;
            }



            PresentModalViewController (picker, true);
        }

我将图像视图添加到部分的标题视图以显示它...我需要这样做 - 但是如果我多次这样做,我会收到内存警告,然后应用程序最终会崩溃。我试图处理我不使用的对象,甚至手动调用 gc ......无济于事。唯一有效的是处理图像和图像视图......但是部分标题反映了违背目的的图像视图处理......如果您需要更多信息,请告诉我......谢谢!

4

1 回答 1

2

如果您需要向用户显示您拍摄的图像(一次不止一张),您应该调整它们的大小以仅使用您在屏幕上实际需要的尺寸(许多全尺寸图像占用大量内存,因为您已经发现,当您只想向用户显示图像时,大部分都不需要)。

如果您还需要原始全尺寸图像,则需要先将其存储到磁盘。

于 2013-06-17T21:06:47.930 回答