簡體   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)

當我從相機( 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文件中聲明它。

希望這可以幫助。

我終於意識到,通過從項目中刪除一些不需要的圖像/資源,我可以運行我的應用程序而不會崩潰... !!! 謝謝所有幫助我的人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM