因内存压力而终止

时间:2014-09-18 09:28:12

标签: ios memory uicollectionview

我有一个集合视图,它从文档目录中加载图像。当我尝试将这些图像添加到集合视图时,应用程序会因内存警告而崩溃。

-(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;
}

我做错了什么?

更新:enter image description here

图像尺寸合适,因为我从手机的相机中取出并保存在文档目录中

更新

我收到了为图片添加thumnails的建议,然后从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;
}

1 个答案:

答案 0 :(得分:1)

您正在加载完整尺寸的图片,但您可能只显示缩略图。然而,事实上,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;
}