gpt4 book ai didi

ios - 当我的设备上运行其他应用程序(2 个或更多应用程序)时,ImagePickerController 使我的应用程序崩溃并显示 "Received Memory Warning"

转载 作者:行者123 更新时间:2023-11-28 17:57:01 25 4
gpt4 key购买 nike

当我从相机 (UIImagePickerController) 拍照时,我的应用程序崩溃并提示“Received Memory Warning”。

我遇到崩溃的情况:如果我的设备上正在运行其他应用程序(2 个或更多),我会遇到崩溃,如果只有一个其他应用程序在运行或没有其他应用程序在我启动我的应用程序之前运行然后根本没有崩溃。

此崩溃仅在我从相机拍摄照片时发生,但当我从照片库中选择照片时,根本没有崩溃。

我正在使用 Xcode 4.3.2 和 ARC

谁能帮我解决这个问题?

这是我正在使用的代码

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex: (NSInteger)buttonIndex 
{

if (buttonIndex < 2)
{
if([UIImagePickerController isSourceTypeAvailable:buttonIndex==0?UIImagePickerControllerSourceTypeCamera:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
[ipc setDelegate:(id<UIImagePickerControllerDelegate,UINavigationControllerDelegate>) self];
[ipc setSourceType:buttonIndex==0?UIImagePickerControllerSourceTypeCamera:UIImagePickerControllerSourceTypePhotoLibrary];
if (buttonIndex == 0)
{
[ipc setAllowsEditing:NO];
}
[ipc setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil]];
[self presentModalViewController:ipc animated:YES];
}
else
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Action Alert" message:@"Camera is not available in this device." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
}



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage];
[NSThread detachNewThreadSelector:@selector(scallImage:) toTarget:self withObject:image];
}




-(void) scallImage:(UIImage *) image
{
CGSize newSize = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
image=nil;
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *cImage = [UIImageJPEGRepresentation(newImage, 0.5) base64EncodedString];
[ImageButton setBackgroundImage:newImage forState:UIControlStateNormal];
[ImageButton setHidden:NO];
//[profileImageButton setBackgroundImage:newImage forState:UIControlStateNormal];
newImage=nil;
}

我的代码有什么问题吗?

最佳答案

尽量避免在该方法中分配 UIImagePickerController。而是在 viewDidLoad 中分配它。将这行代码放在 viewDidLoad 中:

 ipc = [[UIImagePickerController alloc] init];

并首先在 .h 文件中声明它。

希望这对您有所帮助。

关于ios - 当我的设备上运行其他应用程序(2 个或更多应用程序)时,ImagePickerController 使我的应用程序崩溃并显示 "Received Memory Warning",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14273508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com