2012-04-06 51 views
0

当我在iphone上运行此程序后,添加三个图像内存警告来和应用程序崩溃后iphone。所以请帮助我感谢我只是获取图像参考和写入文件,然后从文件路径获取图像。由于UIImagePickerViewController在iphone中获取图像时的内存警告

- (IBAction)addPicsButtonClick:(id)sender 
{ 
    UIImagePickerController * picker = [[[UIImagePickerController alloc] init] autorelease]; 
    picker.delegate = self; 
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;  
    [self presentModalViewController:picker animated:YES];  
} 


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *pathToDocuments=[paths objectAtIndex:0]; 
    AppDelegate *app = [[UIApplication sharedApplication] delegate]; 
    NSData *imageData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"], 0.3f);  
    [imageData writeToFile:[NSString stringWithFormat:@"%@/%d.jpg", pathToDocuments, [app.images count]] atomically:YES];  
    [self dismissModalViewControllerAnimated:YES]; 

} 
+0

保持你的代码addPicsButtonClick在autorelease池可能是可以解决你的问题。 – Leena 2012-04-07 10:39:50

回答

0

我跑了相同的代码在模拟器一个新的应用程序,并没有崩溃的问题或内存不足的警告,它创建的文件。我没有取出[app.images count],而是使用了一个局部变量,因为app.images在此代码中没有设置。所以如果这个变量是垃圾或过度释放,那可能是你的问题。