简体   繁体   English

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

[英]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.

相关问题 为什么我的应用程序崩溃 - 在 iOS 7 上收到内存警告错误 - Why my app crashes - received memory warning error on iOS 7 如何从我的应用程序打开我的设备上的其他应用程序 - How to open other apps on my device from my app 在设备上运行应用程序时出现“收到内存警告” - “Received memory warning” while running app on device 当多个应用程序在后台运行时出现内存警告 - Memory warning when several apps running in background 我的应用启动后,其他应用的音乐停止播放 - Music from other apps stops playing when my app starts 当其他应用程序在Swift中播放音乐时,将我的应用程序静音 - Mute My App When Other Apps are Playing Music in Swift 如果我的 iOS 应用与使用 Core Bluetooth 的设备配对,其他应用是否可以访问该设备? - If my iOS app pairs with a device using Core Bluetooth, will other apps have access to the device? 在我的服务器上上传100多张图像时收到内存警告和应用程序崩溃 - Received memory warning and app crash while uploading more than 100 images on my server 在我的应用内控制其他应用的音乐 - Control music of other apps within my app 当设备从其他应用程序(例如日历)接收警报(警报视图)时,我的应用程序将进入什么状态 - What state my app will enter into when device receiving alerts (alert view) from other apps (e.g. calendar)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM