我想要做的是在我的整個應用程序中始終使用UIImagePickerController的相同實例,因爲我不想分配和銷燬我的每次用戶拍照時選擇器。該應用程序使用一個選擇器和名爲pictureA的UIImageView來顯示來自iPhone相冊的圖片選擇或者用相機拍攝的圖片。正確使用UIImagePickerController同時使用相機拍攝照片或從iphone庫中選擇它們
在開始我allock我選擇器視圖控制器是這樣的:
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
[self addSubview:imagePickerController.view];
當我從揀貨機圖片,並把它在我的UIImageView:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// newImage is a UIImage do not try to use a UIImageView
UIImage *newImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
[pictureA removeFromSuperview];
CGRect myImageRect = CGRectMake(30.0f, 38.0f, 125.0f, 125.0f);
pictureA = [[UIImageView alloc] initWithFrame:myImageRect];
pictureA.contentMode= UIViewContentModeScaleAspectFit;
[pictureA setImage:newImage];
pictureA.opaque = YES;
[self addSubview:pictureA];
[newImage release];
[picker dismissModalViewControllerAnimated:YES];
[picker.view setHidden:YES];
}
當選擇從iPhone的圖片庫:
- (void) chooseImageFromLibrary {
[imagePickerController.view setHidden:NO];
imagePickerController.allowsEditing = YES;
[imagePickerController viewWillAppear:YES];
[imagePickerController viewDidAppear:YES];
}
取消:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker.view setHidden:YES];
}
- (void) takePicture {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[(MainScene*)[melonGame actualScene] setCameraDissabled:YES];
return;
}
[imagePickerController.view setHidden:NO];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = YES;
[imagePickerController takePicture];
[imagePickerController viewWillAppear:YES];
[imagePickerController viewDidAppear:YES];
}
當我拍攝第一張照片時,應用程序將正常工作,但是當我嘗試拍攝另一張照片或從我的照片庫中選擇另一張照片時,它看起來與我上次選擇的照片完全一樣(所有照片屏幕和一個不活動的菜單可以選擇並取消在底部)。我的應用程序只能正常工作,如果我從我的視圖中刪除控制器,並在用戶每次拍攝照片時進行分配。有沒有什麼辦法可以「清理」或「重新啓動」選取器,而不會釋放它並從視圖中刪除它?
我也不確定如果我可以實例一次UIImageView(pictureA)存儲imagePickerController拍攝的圖像,並修改它每次用戶拍照或從專輯中選擇一個,我懷疑它是每當用戶拍攝照片時都不正確地銷燬它並重新分配它。任何建議?,我做的任何事情有關內存管理或使用UIImagePickerController和UIImageView?
謝謝!