我们正在做一个使用UIImagePickerController的照片应用程序。为了只维护控制器的一个实例,我们在appdelegate中声明了它。但即使这样做,内存泄漏仍然存在。任何人都可以帮助我们 这是我们的代码
在AppDelegate.h中
UIImagePickerController *imagePicker;
@property (nonatomic, retain) UIImagePickerController *imagePicker;
在AppDelegate.m中
@synthesize imagePicker;
和applicationDidFinishLaunching事件
imagePicker = [[[UIImagePickerController alloc] init] autorelease];
需要在CameraView中调用此UIImagePicker
在CameraView.h中
RedDawnMediaAppDelegate *appDel;
@property (nonatomic, retain) RedDawnMediaAppDelegate *appDel;
在CameraView.m中
@synthesize appDel;
并在viewDidLoad
中appDel=(RedDawnMediaAppDelegate*)[[UIApplication sharedApplication] delegate];
appDel.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Hide the camera controls:
appDel.imagePicker.showsCameraControls = NO;
appDel.imagePicker.navigationBarHidden = YES;
appDel.imagePicker.delegate = self;
// Make the view full screen:
appDel.imagePicker.wantsFullScreenLayout = YES;
appDel.imagePicker.cameraViewTransform = CGAffineTransformScale(appDel.imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
// Now incert our overlay view (it has to be here cuz it's a modal view):
appDel.imagePicker.cameraOverlayView = overlayView;
[self presentModalViewController:appDel.imagePicker animated:YES];
在didFinishPickingMediaWithInfo
中- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[appDelegate.imagePicker dismissModalViewControllerAnimated:YES];
uploadImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[uploadImage retain];
NSData *imageData = UIImageJPEGRepresentation(uploadImage, 0.5);
//[uploadImage release];
UIImage *image=[UIImage imageWithData:imageData];
self.view.frame = CGRectMake(0, 0, 320, 480);
self.wantsFullScreenLayout = YES;
objCameraImageView = [[CameraImageView alloc] initWithNibName:@"CameraImageView" bundle:[NSBundle mainBundle]];
[objCameraImageView setParentController:self];
objCameraImageView.cameraImage = image;
[image release];
uploadImage = nil;
[self.view addSubview:objCameraImageView.view];
[self release];
}
拍摄照片后,我们将在另一个视图中显示预览。所以我们多次调用Cameraview,导致内存泄漏帮助。
答案 0 :(得分:0)
首先,您不必担心只维护一个imagepickercontroller实例。从相机获取图像比您正在做的更简单。
请在此处查看我的帖子:
How to take picture from Camera & saved in Photo Gallery by programmatically?
因为我在完成获取图像时释放了选择器。不会有内存泄漏!