由于内存压力而终止

阿宾·乔治(Abin George)

我有一个收集视图,该视图从文档目录加载图像。当我尝试将这些图像加载到collectionview时,应用程序崩溃并显示内存警告。

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImage *cellImage;
if (indexPath.section == 0) {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *folderPath = [documentsDirectoryPath  stringByAppendingPathComponent:@"Photos"];  // subDirectory
    NSString *filePath;    
    if ([[NSFileManager defaultManager] fileExistsAtPath:folderPath])
        filePath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Photo%d",indexpath.row]];

    cellImage = [UIImage imageWithContentsOfFile:filePath];

} else if (indexPath.section == 1){
    cellImage = [UIImage imageNamed:@"btn_vid.png"];
} else if (indexPath.section == 2){
    cellImage = [UIImage imageNamed:@"btn_mic.png"];
} else if (indexPath.section == 3){
    cellImage = [UIImage imageNamed:@"btn_1.png"];
}
UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
[imageView setImage:cellImage];

return cell;
}

我究竟做错了什么?

更新: 在此处输入图片说明

图像尺寸合适,因为我是从手机的摄像头上取下来并保存在文档目录中的

更新:

我已经收到有关为图像添加缩略图的建议,然后从Airsource Ltd.加载它们我使用此代码生成了缩略图图像,而不是他的图像,它起作用了。但是我想知道,哪个更好。

+(UIImage*) generateThumbnailFromImage:(UIImage*)theImage
{
UIImage * thumbnail;
CGSize destinationSize = CGSizeMake(80,80);

UIGraphicsBeginImageContext(destinationSize);
[theImage drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return thumbnail;
}
空气资源有限公司

您正在加载完整尺寸的图像,但是大概只显示了一个缩略图。但是,就目前情况而言,UIImage需要将完整的,解压缩的图像保留在内存中,以便显示按比例缩小的版本,这效率很低。

要生成缩略图,请以NSData格式加载文件,然后使用标准的Apple代码生成缩略图。不要并行执行太多操作,否则您将遇到完全相同的问题

CGImageRef MyCreateThumbnailImageFromData (NSData * data, int imageSize)
{
    CGImageRef        myThumbnailImage = NULL;
    CGImageSourceRef  myImageSource;
    CFDictionaryRef   myOptions = NULL;
    CFStringRef       myKeys[3];
    CFTypeRef         myValues[3];
    CFNumberRef       thumbnailSize;

   // Create an image source from NSData; no options.
   myImageSource = CGImageSourceCreateWithData((CFDataRef)data,
                                               NULL);
   // Make sure the image source exists before continuing.
   if (myImageSource == NULL){
        fprintf(stderr, "Image source is NULL.");
        return  NULL;
   }

   // Package the integer as a  CFNumber object. Using CFTypes allows you
   // to more easily create the options dictionary later.
   thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize);

   // Set up the thumbnail options.
   myKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
   myValues[0] = (CFTypeRef)kCFBooleanTrue;
   myKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
   myValues[1] = (CFTypeRef)kCFBooleanTrue;
   myKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
   myValues[2] = (CFTypeRef)thumbnailSize;

   myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
                   (const void **) myValues, 2,
                   &kCFTypeDictionaryKeyCallBacks,
                   & kCFTypeDictionaryValueCallBacks);

  // Create the thumbnail image using the specified options.
  myThumbnailImage = CGImageSourceCreateThumbnailAtIndex(myImageSource,
                                          0,
                                          myOptions);
  // Release the options dictionary and the image source
  // when you no longer need them.
  CFRelease(thumbnailSize);
  CFRelease(myOptions);
  CFRelease(myImageSource);

   // Make sure the thumbnail image exists before continuing.
   if (myThumbnailImage == NULL){
         fprintf(stderr, "Thumbnail image not created from image source.");
         return NULL;
   }

   return myThumbnailImage;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

由于内存压力而终止,使用的内存不足15MB

来自分类Dev

当我使用Photos.app时,我的iOS应用由于内存压力而终止

来自分类Dev

iOS 7应用因内存压力而终止

来自分类Dev

我该如何解决一个较早启动但现在“由于内存压力而终止”的应用程序?

来自分类Dev

由于内存错误而终止的应用

来自分类Dev

应用程序由于内存压力而不断崩溃

来自分类Dev

iPhone应用程序由于内存压力而崩溃

来自分类Dev

iPhone应用程序由于内存压力而崩溃

来自分类Dev

应用程序由于内存压力而不断崩溃

来自分类Dev

由于“内存过多”,App Engine进程随机终止

来自分类Dev

应用程序崩溃并显示消息“由于内存错误而终止”

来自分类Dev

GPUImage和GPUImageView:应用由于内存错误而终止

来自分类Dev

来自调试器的XCode消息:由于使用UIImageJPEGRepresentation时由于内存问题而终止

来自分类Dev

Ionic App:收到内存警告。来自调试器的消息:由于内存问题而终止

来自分类Dev

ARC iOS中的内存压力

来自分类Dev

终止应有的记忆压力

来自分类Dev

来自调试器的Ios Xcode消息:由于内存问题而终止

来自分类Dev

Xcode 7 / Swift 2.1“来自调试器的消息:由于内存问题而终止”

来自分类Dev

由于内存不足而导致应用程序终止时,不会调用onLowMemory()

来自分类Dev

当我使用Photos.app时,我的iOS应用由于内存不足而终止

来自分类Dev

Xcode 9 Swift 4 由于内存问题而终止(返回一个单元格)

来自分类Dev

如何因内存压力而调试iOS崩溃

来自分类Dev

我该如何解决一个较早启动但现在“由于内存不足而终止”的应用程序?

来自分类Dev

我可以暂停一个进程(“压力”)来模拟内存压力减去CPU成本吗?

来自分类Dev

如何在python中模拟cpu和内存压力

来自分类Dev

为什么Elasticsearch Cluster JVM内存压力不断增加?

来自分类Dev

使用AFNetworking下载多个文件时出现内存压力问题

来自分类Dev

Node JS应用程序内存压力上升

来自分类Dev

由于语句超时,PostgreSQL查询突然终止

Related 相关文章

  1. 1

    由于内存压力而终止,使用的内存不足15MB

  2. 2

    当我使用Photos.app时,我的iOS应用由于内存压力而终止

  3. 3

    iOS 7应用因内存压力而终止

  4. 4

    我该如何解决一个较早启动但现在“由于内存压力而终止”的应用程序?

  5. 5

    由于内存错误而终止的应用

  6. 6

    应用程序由于内存压力而不断崩溃

  7. 7

    iPhone应用程序由于内存压力而崩溃

  8. 8

    iPhone应用程序由于内存压力而崩溃

  9. 9

    应用程序由于内存压力而不断崩溃

  10. 10

    由于“内存过多”,App Engine进程随机终止

  11. 11

    应用程序崩溃并显示消息“由于内存错误而终止”

  12. 12

    GPUImage和GPUImageView:应用由于内存错误而终止

  13. 13

    来自调试器的XCode消息:由于使用UIImageJPEGRepresentation时由于内存问题而终止

  14. 14

    Ionic App:收到内存警告。来自调试器的消息:由于内存问题而终止

  15. 15

    ARC iOS中的内存压力

  16. 16

    终止应有的记忆压力

  17. 17

    来自调试器的Ios Xcode消息:由于内存问题而终止

  18. 18

    Xcode 7 / Swift 2.1“来自调试器的消息:由于内存问题而终止”

  19. 19

    由于内存不足而导致应用程序终止时,不会调用onLowMemory()

  20. 20

    当我使用Photos.app时,我的iOS应用由于内存不足而终止

  21. 21

    Xcode 9 Swift 4 由于内存问题而终止(返回一个单元格)

  22. 22

    如何因内存压力而调试iOS崩溃

  23. 23

    我该如何解决一个较早启动但现在“由于内存不足而终止”的应用程序?

  24. 24

    我可以暂停一个进程(“压力”)来模拟内存压力减去CPU成本吗?

  25. 25

    如何在python中模拟cpu和内存压力

  26. 26

    为什么Elasticsearch Cluster JVM内存压力不断增加?

  27. 27

    使用AFNetworking下载多个文件时出现内存压力问题

  28. 28

    Node JS应用程序内存压力上升

  29. 29

    由于语句超时,PostgreSQL查询突然终止

热门标签

归档