gpt4 book ai didi

ios - 由于内存压力而终止

转载 作者:行者123 更新时间:2023-11-29 02:40:01 26 4
gpt4 key购买 nike

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

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

图片大小合适,因为我是用手机相机拍的,保存在文档目录里

更新:

我收到了为图像添加缩略图然后从 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;
}

关于ios - 由于内存压力而终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25908964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com