简体   繁体   English

在iPhone中使用UIImagePickerControllerSourceTypeCamera捕获图像10或11次后收到内存警告

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

Hi I receive memory warning whenever I am using camera. 您好,每当我使用相机时,都会收到内存警告。

The error is like this: 错误是这样的:

"Receive memory warning..."

And the code is: 代码是:

-(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];
}

How can i handle this?please help after taking picture i crop the image and save in application like this: 我该如何处理?拍照后请帮忙裁剪图像并保存在应用程序中,如下所示:

- (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"]]);




}

Please help me. 请帮我。 I want to remove all warnings. 我要删除所有警告。

You're leaking the UIImagePickerController . 您正在泄漏UIImagePickerController Autorelease it on creation or release it after dismissModalViewControllerAnimated . 创建时自动释放它,或者在dismissModalViewControllerAnimated之后释放它。

You may still get memory warnings, photos can be enormous, especially on the iPhone 4 and at a certain point you have two of them in memory: the UIImage and the autoreleased PNG. 您可能仍然会收到内存警告,照片可能非常庞大,尤其是在iPhone 4上,并且在某些时候,内存中有两个: UIImage和自动发布的PNG。

PS you don't seem to be using size and cropRect so you could delete them. PS,您似乎并没有使用sizecropRect因此可以删除它们。

Release your alert view. 释放警报视图。 In general release any object you alloc. 通常释放您分配的任何对象。 In case you have a retain property then assign a auto release object to it. 如果您具有保留属性,则为其分配一个自动释放对象。

When you get a memory warning your view controller method - (void)didReceiveMemoryWarning is called. 当收到内存警告时,您的视图控制器方法- (void)didReceiveMemoryWarning被调用。 Here you will have to release any unwanted objects which you have cached. 在这里,您将必须释放已缓存的所有不需要的对象。 Typically that would be some images, views in stack etc. 通常,这将是一些图像,堆栈视图等。

Also check if you are having appropriate dealloc for objects in your modal view controller. 还要检查是否在模态视图控制器中为对象分配了适当的dealloc。

Are you implementing -imagePickerController:didFinishPickingMediaWithInfo: ? 您是否正在实现-imagePickerController:didFinishPickingMediaWithInfo: The method you've implemented has been deprecated. 您已实现的方法已被弃用。 You should use the other method even for images. 您甚至应该对图像使用其他方法。 What are you doing with the recorded videos? 您正在处理录制的视频吗?

On a side note, the following code – 附带说明一下,以下代码–

@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];
}

should be 应该

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]
}

Now smarter thing would be to disable button 2 which would be 现在更聪明的是禁用按钮2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM