开发者

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

开发者 https://www.devze.com 2023-03-07 05:07 出处:网络
Hi I receive memory warning whenever I am using camera. The error is like this: \"Receive memory warning...\"

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 
        {
            p开发者_高级运维icker.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. Autorelease it on creation or release it after 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.

P.S. you don't seem to be using size and cropRect so you could delete them.


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. 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.


Are you implementing -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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号