开发者

Memory issue with perform selector - iPhone

i got a problem and i don't know how to solve it. this is what i try to do: the user picks an image from his album on his iphone. while the image will be saved in the application folder, a actionsheet should popup with an activity indicator on it till the saving process is done. below you can see how i tried to do it, but i think there my code causes a memory problem (EXC_BAD_ACCESS), but i can't fix it.

when i debug, i found out, that both methods which are listed below will be called in a loop, that's what confuses me the most.

thanks in advance for your help.

sean

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage* img = [info objectForKey:UIImagePickerControllerOriginalImage];
    img = [self rotateImage:img byOrientationFlag:img.imageOrientation];

    NSMutableArray *args = [[NSMutableArray alloc] init];

    activityLoadImage = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(-17.5, -11, 35, 35)];
    activityLoadImage.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    activityLoadImage.autoresizingMask = (UIViewAutoresizingFlexibleLeftMarg开发者_如何学Cin |
                                      UIViewAutoresizingFlexibleRightMargin |
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin);


    actionSheetLoadImage = [[UIActionSheet alloc] initWithTitle:@""
                                                   delegate:self
                                          cancelButtonTitle:nil
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:nil];
    actionSheetLoadImage.actionSheetStyle = UIBarStyleBlackOpaque;

    [actionSheetLoadImage setMessage:NSLocalizedString(@"_LoadImage", @"loading image")];
    [actionSheetLoadImage addSubview:activityLoadImage];
    [actionSheetLoadImage showInView:self.view];
    [actionSheetLoadImage setBounds:CGRectMake(0,-105,320,720)];

    [args insertObject:picker atIndex:0];
    [args insertObject:img atIndex:1];
    [args insertObject:[info objectForKey:UIImagePickerControllerOriginalImage] atIndex:2];

    [activityLoadImage performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];
    [self performSelector:@selector(saveImage:) withObject:args afterDelay:0.1];
    [args release];
}

- (void)saveImage:(NSMutableArray *)arguments
{   
    UIImagePickerController *picker = [[arguments objectAtIndex:0] retain];
    UIImage *img = [[arguments objectAtIndex:1] retain];
    UIImage *orgImg = [[arguments objectAtIndex:2] retain];

    // writes the image to specific location with the given file name
    NSData *imageData = [self getImageData:img];
    [imageData writeToFile:[self getImagePath] atomically:YES];

    // writes the small image to specific location with the given file name
    imageData = [self getSmallImageData:img];
    [imageData writeToFile:[self getSmallImagePath] atomically:YES];

    // sets UIImage for the UIImageView
    [self setViewInfo];
    lblNoImage.hidden = TRUE;

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    [self.tableView reloadData];

    [activityLoadImage performSelectorOnMainThread:@selector(stopAnimating) withObject:nil wait    UntilDone:NO];
    [actionSheetLoadImage dismissWithClickedButtonIndex:0 animated:YES];
    [activityLoadImage release];
    [actionSheetLoadImage release];
}


You're passing args into saveImage and then releasing it. Since the method is waiting 0.1 seconds before executing by this time args has already been released. Try not releasing args to see if that fixes your problem then think about a better way to manage the memory, possibly retain it as a property of the class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜