[英]ImagePickerController crashes my app saying “Received Memory Warning” when there are other apps running on my device (2 or more apps)
My App crashes saying " Received Memory Warning
" when i take a picture from camera ( UIImagePickerController
). 当我从相机(
UIImagePickerController
)拍照时,我的应用程序崩溃并说“ Received Memory Warning
”。
Scenario when i get the crash : If I have other apps running on my device(2 or more), I am getting the crash and if there is only one other app running or no other apps running before I launch my app then there are no crashes at all. 发生崩溃的情况 :如果我的设备上正在运行其他应用程序(2个或更多),则崩溃了;如果在启动我的应用程序之前只有一个其他应用程序正在运行,或者没有其他应用程序在运行,则没有崩溃完全崩溃。
This crash happens only when i take the picture from my camera but when i choose the picture from photo library there is no crash at all. 仅当我从相机拍摄照片时才发生此崩溃,但是当我从照片库中选择照片时,根本没有崩溃。
I am using Xcode 4.3.2 and I am using ARC . 我正在使用Xcode 4.3.2,并且正在使用ARC 。
Can any one help me on this? 谁可以帮我这个事?
This is the code i am using 这是我正在使用的代码
-(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;
}
Is there any thing wrong in my code? 我的代码有什么问题吗?
Try avoiding to allocate UIImagePickerController in that method. 尝试避免在该方法中分配UIImagePickerController。 Instead allocate it in viewDidLoad.
而是在viewDidLoad中分配它。 Put this line of code in viewDidLoad:
将以下代码行放入viewDidLoad中:
ipc = [[UIImagePickerController alloc] init];
And have it declared first in .h file. 并首先在.h文件中声明它。
Hope this helps. 希望这可以帮助。
I finally realized that by removing some of the unwanted images/resources from the project I am able to run my app without crashes ...!!! 我终于意识到,通过从项目中删除一些不需要的图像/资源,我可以运行我的应用程序而不会崩溃... !!! Thanks everyone who helped me !
谢谢所有帮助我的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.