2

Ben belge dizininde görüntüleri kaydetme ve alma ve ekranda görüntülemeye son derece bağımlı bir uygulama üzerinde çalışıyorum.Belge dizini uygulamasında saklanan ağır görüntüler nedeniyle bellek uyarısı alıyor. Ve uygulama çöktü

Koleksiyon görünümünde 5 - 6 görüntüyü görüntülediğimde, ap yavaşlar ve aniden bellek uyarısı alır ve işlev ve uygulama çökmelerini durdurur.

Verileri

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
      cellForItemAtIndexPath:(NSIndexPath *)indexPath 

görüntülemek için aşağıdaki kod kullanıyorum { DocumentCollectionViewCell * hücre = [CollectionView'ın dequeueReusableCellWithReuseIdentifier: "DocumentCollectionViewCell" @ forIndexPath: indexPath];

if(cell!=nil){ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DocumentCollectionViewCell" forIndexPath:indexPath]; 

} 
Documents *documents = [arrTableData objectAtIndex:indexPath.row]; 
cell.imgView.contentMode = UIViewContentModeScaleAspectFit; 
cell.imgContentView.layer.cornerRadius = 4.0f; 
cell.imgContentView.layer.masksToBounds = YES; 
cell.imgContentView.layer.borderColor = [UIColor lightGrayColor].CGColor; 
cell.imgContentView.layer.borderWidth = .4f; 

[cell.btnLockOrUnlock addTarget:self action:@selector(lockAction:) forControlEvents:UIControlEventTouchUpInside]; 
cell.btnLockOrUnlock.tag = indexPath.row; 
// set count 
cell.lblCount.text =[NSString stringWithFormat:@"%@ Page",documents.imageCount]; 
cell.imgView.layer.cornerRadius = 6.0f; 
cell.imgView.layer.masksToBounds = YES; 
newDocDate = documents.issueDate; 
// set image 
NSString * passcode = documents.passcode; 
if(passcode.length>3){ 
    cell.bluredView.hidden = NO; 
    [cell.btnLockOrUnlock setImage:[UIImage imageNamed:@"lockWhite"] forState:UIControlStateNormal]; 
} 
else{ 
    [cell.btnLockOrUnlock setImage:[UIImage imageNamed:@"unlockWhite"] forState:UIControlStateNormal]; 
    cell.bluredView.hidden = YES; 
} 

[cell.btnSelect addTarget:self action:@selector(cellSelectAction:) forControlEvents:UIControlEventTouchUpInside]; 
cell.btnSelect.tag = indexPath.row; 
NSString *title = documents.title; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory 
NSString * docDirectoryPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:title]; 

//-----------------path of document ------------------ 

NSString *filePath = [docDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",0]]; 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 
int i =0; 
while (!fileExists) { 
    filePath = [docDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",i]]; 
    fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 
    i++; 
} 
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:filePath], NULL); 
// Create thumbnail options 
CFDictionaryRef options = (__bridge CFDictionaryRef) @{ 
                 (id) kCGImageSourceCreateThumbnailWithTransform : @YES, 
                 (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES, 
                 (id) kCGImageSourceThumbnailMaxPixelSize : @(cell.imgView.frame.size.height) 
                 }; 
// Generate the thumbnail 
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); 
UIImage* uiImage = [[UIImage alloc] initWithCGImage:thumbnail]; //<--CRASH 
cell.DocName.text = documents.docName; 


//-----------------display image on cell------------------ 


cell.imgView.image = uiImage; 
uiImage = nil; 
uiImage = NULL; 
documents = nil; 
documents = nil; 
title = nil; 
thumbnail = nil; 
src = nil; 
options = nil; 
filePath = nil; 
paths = nil; 
documentsDirectory = nil; 
docDirectoryPath = nil; 
return cell; 

}

ben nil tüm nesneleri kuruyorum.

Ben görüntüleri

kaydetmek için aşağıdaki kodu kullanıyorum
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory 
folderName = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:folderName]; 

NSData * pngData = UIImagePNGRepresentation (arrImages [i]); NSString * filePath = [folderName stringByAppendingPathComponent: [NSString stringWithFormat: @ "image% d.png", i]]; // dosya adını ekleyin [pngData writeToFile: filePath atomically: YES]; // Dosyayı yaz

Orijinal görüntüyü, herhangi bir sıkıştırma veya yeniden boyutlandırma olmadan belge dizininden kaydediyorum.

Sorunu anlayamıyorum lütfen yardım edin.

Şimdiden teşekkürler.

cevap

1

Bellek sızıntılarından kaynaklanıyor gibi görünüyor, uygulamalarınızı enstrümanlarla kontrol edin ve aracınız yok.

CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:filePath], NULL); 
// Create thumbnail options 
CFDictionaryRef options = (__bridge CFDictionaryRef) @{ 
                 (id) kCGImageSourceCreateThumbnailWithTransform : @YES, 
                 (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES, 
                 (id) kCGImageSourceThumbnailMaxPixelSize : @(cell.imgView.frame.size.height) 
                 }; 
// Generate the thumbnail 
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); 
UIImage* uiImage = [[UIImage alloc] initWithCGImage:thumbnail]; //<--CRASH 

CFFoundation nesneleri (oluşturursanız ya da onu serbest gerekir kopya) benzer bellek yönetimi kuralları takip ARC (ARC doesn't manage core foundation objects) önce Obj C.
Gördüğünüz kodda, CGImageRef ve CGImageSourceRef yayınlamadığını görüyorum, bu iki sızıntı ve büyük olasılıkla kilitlenme yaratır.
Toplama görünümü hücreleri geri dönüştürülür, böylece bellekte açılan görüntü sayısı temelde ekranda gördüğünüz hücrelerin sayısıdır, bunlar sizin kazınızın sebebi olmalıdır.

+1

Teşekkürler Andrea bana yardımcı oldu :) –