簡體   English   中英

在iPhone中使用UIImagePickerControllerSourceTypeCamera捕獲圖像10或11次后收到內存警告

[英]Receive memory warning after 10 or 11 times capturing image using UIImagePickerControllerSourceTypeCamera in iphone

您好,每當我使用相機時,都會收到內存警告。

錯誤是這樣的:

"Receive memory warning..."

代碼是:

-(void) getPhoto{

    GameAppdelegate *appDelegate = (GameAppdelegate *)[[UIApplication sharedApplication]delegate];

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;

    ///////////////////////////////////photolibrary//////////////////////////////
    if([appDelegate.photoselection isEqualToString:@"User Pressed Button 1\n"])
    {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        if(appDelegate.sound == 1)
        {
            [classObj ButtonSound];
        }
    }
    ///////////////////////////////////Camera//////////////////////////////

    else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 2\n"]) 
    {

        @try 
        {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        @catch (NSException * e)
        {
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                         message:@"Please try again"
                                                        delegate:self 
                                               cancelButtonTitle:nil 
                                               otherButtonTitles:@"ok", nil];
            [av show];

        }
        if(appDelegate.sound == 1)
        {
            [classObj ButtonSound];
        }
    }
    ///////////////////////////////////Cancel//////////////////////////////


    else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 3\n"]) 
    {
        if(appDelegate.sound == 1)
            [classObj ButtonSound];
        return;
    }
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

我該如何處理?拍照后請幫忙裁剪圖像並保存在應用程序中,如下所示:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

    iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];

    if(appDelegate.sound == 1)
    {
        [classObj ButtonSound];
    }

    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = image;
    CGSize size = [imageView.image size];
    CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);

    NSValue *cropRectValue = [editingInfo objectForKey:@"UIImagePickerControllerCropRect"];
    cropRect = [cropRectValue CGRectValue];

    appDelegate.slectedimage = image; 
    imageView.hidden = YES;

    if( [appDelegate.Name length] != 0 && max_att == 15)
    {
        btnNotDone.hidden   = YES;
        btnDone.enabled = YES;
    }

    //IMAGE SAVE IN DOCUMENTS//////
    [UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
    // Show the current contents of the documents folder
    CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);




}

請幫我。 我要刪除所有警告。

您正在泄漏UIImagePickerController 創建時自動釋放它,或者在dismissModalViewControllerAnimated之后釋放它。

您可能仍然會收到內存警告,照片可能非常龐大,尤其是在iPhone 4上,並且在某些時候,內存中有兩個: UIImage和自動發布的PNG。

PS,您似乎並沒有使用sizecropRect因此可以刪除它們。

釋放警報視圖。 通常釋放您分配的任何對象。 如果您具有保留屬性,則為其分配一個自動釋放對象。

當收到內存警告時,您的視圖控制器方法- (void)didReceiveMemoryWarning被調用。 在這里,您將必須釋放已緩存的所有不需要的對象。 通常,這將是一些圖像,堆棧視圖等。

還要檢查是否在模態視圖控制器中為對象分配了適當的dealloc。

您是否正在實現-imagePickerController:didFinishPickingMediaWithInfo: 您已實現的方法已被棄用。 您甚至應該對圖像使用其他方法。 您正在處理錄制的視頻嗎?

附帶說明一下,以下代碼–

@try 
{
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
@catch (NSException * e)
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                message:@"Please try again"
                                               delegate:self 
                                      cancelButtonTitle:nil 
                                      otherButtonTitles:@"ok", nil];
    [av show];
}

應該

if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT" 
                                                 message:@"Camera isn't available"
                                                delegate:self 
                                       cancelButtonTitle:nil 
                                       otherButtonTitles:@"ok", nil];
    [av show];
    [av release]
}

現在更聰明的是禁用按鈕2

if ( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
    button2.enabled = N0;
}

暫無
暫無

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

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