developerWab 2013-04-28 03:33 采纳率: 0%
浏览 1410

好像是内存引起的崩溃,请帮忙看看

开发了一个程序,从照片数据库中导入相片。在导入的时候占了很多内存,第160个照片的时候系统崩溃了。

不知道是不是内存占用太多引起的。

谢谢

    NSDate *date = [NSDate date];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"DD_MM_YY_HH_mm_ss"];
    NSString *dateString = [dateFormat stringFromDate:date];
    int random=rand();
    // create new file names
    NSString *fileName=[[NSString alloc] initWithFormat:@"%d%@",random,dateString];
    NSString *s_fileName=[[NSString alloc]  initWithFormat:@"small_%d%@",random,dateString];
    // where to save this image
    NSString *imagePath=[self.documentsDirectory  stringByAppendingPathComponent:fileName];
    NSString *s_imagePath=[self.documentsDirectory stringByAppendingPathComponent:s_fileName];
    NSLog(@"type: %@",[rep.url pathExtension]);

    if ([[rep.url pathExtension] isEqualToString:@"JPG"]) {
        // its jpg
        NSData *rawImage=UIImageJPEGRepresentation(image, 1.0);
        NSData *thumb=UIImageJPEGRepresentation(newImage, 1.0);
        image=nil;
        newImage=nil;
        date=nil;
        dateFormat=nil;
        dateString=nil;
        random=nil;

        // Create the images
        [rawImage writeToFile:imagePath atomically:YES];
        [thumb writeToFile:s_imagePath atomically:YES];
        imagePath=nil;
        s_imagePath=nil;
        rawImage=nil;
        thumb=nil;
    }
    if ([[rep.url pathExtension] isEqualToString:@"PNG"]) {
        // its PNG
        // save it in NSData to it can be saved in the directory
        NSData *rawImage=UIImagePNGRepresentation(image);
        NSData *thumb=UIImagePNGRepresentation(newImage);
        image=nil;
        newImage=nil;
        date=nil;
        dateFormat=nil;
        dateString=nil;
        random=nil;

        // Create the images
        [rawImage writeToFile:imagePath atomically:YES];
        [thumb writeToFile:s_imagePath atomically:YES];
        imagePath=nil;
        s_imagePath=nil;
        rawImage=nil;
        thumb=nil;
    }
    // Now save it to database
    [albumObject savePhoto:fileName toFID:PassedID toName:s_fileName  albumName:singletonObj.currentAlbum photosName:singletonObj.currentPhotos];
    [display_photos addObject:[MWPhoto photoWithFilePath:[self.documentsDirectory  stringByAppendingPathComponent:fileName]]];
    UIImage *img=[UIImage imageWithContentsOfFile:[self.documentsDirectory  stringByAppendingPathComponent:s_fileName]];
    [collection_photos addObject:img];
    img=nil;
    NSArray *pObj=[[NSArray alloc] initWithObjects:fileName,s_fileName, nil];
    [photos addObject:pObj];

    pObj=nil;
    fileName=nil;
    s_fileName=nil;
} j=nil;

// Update the album photo count to reflect the changes
[albumObject updateAlbumCount:PassedID photocount:[photos count]  albumName:singletonObj.currentAlbum];
photos=[albumObject getPhotos:PassedID photosName:singletonObj.currentPhotos];
// End the loop here ***************************************************************
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.navigationItem setHidesBackButton:NO];
[self.navigationItem.rightBarButtonItem setEnabled:YES];
});
});

}

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-21 17:25
    关注

    ChatGPT尝试为您解答,仅供参考
    看起来你的代码正在从照片数据库中导入照片,并将这些照片保存到文件系统中。在这个过程中,你正在使用UIImage对象来存储照片的数据。


    这个问题的原因可能是你的代码在导入大量照片时耗尽了可用的内存。当你的应用程序达到最大内存限制时,就会发生崩溃。

    要解决这个问题,你可以尝试以下方法:

    • 在每次读取照片后,立即释放UIImage对象,这样可以释放内存。
    • 使用流式读取,只读取一个照片的一部分,而不是整个照片。这样可以减少内存使用量。
    • 使用图像编解码器,可以让你的应用程序更有效地处理图像数据。
    • 对于导入的每个照片,检查它的大小,并在必要时压缩它以减少内存使用。
    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。