我正在尝试在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);
}
我将imageview添加到某个部分的标题视图中以显示它......我需要这样做 - 但是如果我多次这样做,我会收到内存警告,然后应用程序最终会崩溃。我试图处理我没有使用的对象,甚至手动调用gc ......无济于事。唯一有效的是处理图像和图像视图...但是部分标题反映了图像视图处理,这违背了目的...如果您需要更多信息,请告诉我...谢谢!
答案 0 :(得分:2)
如果您需要向用户显示您拍摄的图像(一次不止一个),您应该调整它们的大小以仅使用屏幕上实际需要的尺寸(许多全尺寸图像需要很多的内存,正如你已经发现的那样,当你只想向用户展示图像时,大部分都不需要。)
如果您还需要原始的全尺寸图像,则需要先将其存储到磁盘上。