[英]Terminating due to Memory pressure
我有一個收集視圖,該視圖從文檔目錄加載圖像。 當我嘗試將這些圖像加載到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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.