2017-10-12 59 views
0

當我嘗試保存超過10個圖像到文檔目錄由於內存問題導致崩潰。從相機膠捲保存多個圖像到文件目錄由於內存問題墜毀

-(void) importArrayDictionary: (NSArray *) arrayDictionary ToFolder: (NSString *) folderDestination { 

_requestOptions = [[PHImageRequestOptions alloc] init]; 
_requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact; 
_requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; 
_requestOptions.synchronous = true; 

//kch-data 
dispatch_queue_t importQueue = dispatch_queue_create("importQueue", NULL); 
dispatch_async(importQueue, ^{ 





    for(PHAsset *asset in arrayDictionary){ 
     //dispatch_async(dispatch_get_main_queue(), ^{ 
     [self createFileWithName: [self generateNewFileName:[self getTypeFromDictionary:asset]] Folder: folderDestination Item:asset]; 
     //}); 

    } 



    //Notify delegate 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     if (self.delegate != nil && [self.delegate respondsToSelector:@selector(pgDataSourceDidFinishImportingFiles:)]) 
      [self.delegate pgDataSourceDidFinishImportingFiles: self]; 
    }); 
}); 
} 

這是我用來保存的代碼。我從主圖像製作3個不同大小的圖像,並將這些圖像保存到文檔目錄中。最後將主圖像保存到文檔目錄。完成保存一個圖像後,它不釋放內存。我認爲這是問題。

-(NSString *) createFileWithName: (NSString *) name Folder: (NSString *) folder Item: (PHAsset *) aDicItem {//kch-data 



__block UIImage *_imageThumb; 

// 
PHImageManager *manager = [PHImageManager defaultManager]; 
[manager requestImageForAsset:aDicItem 
        targetSize:PHImageManagerMaximumSize 
        contentMode:PHImageContentModeDefault 
         options:_requestOptions 
       resultHandler:^void(UIImage *image, NSDictionary *info) { 
        _imageThumb = image; 

       }]; 

//UIImage *_imageThumb = [aDicItem objectForKey:@"UIImagePickerControllerOriginalImage"]; 
//Thumb110 
//CGSize size = _imageThumb.size; 
UIImage *thumbImage110 = [_imageThumb thumbnailImage:200 transparentBorder:0 cornerRadius:0 interpolationQuality: kCGInterpolationHigh]; 

//thumbImage110 = [_imageThumb imageWithImage:_imageThumb scaledToFillSize:size]; 
[UIImageJPEGRepresentation(thumbImage110, 0.75) writeToFile: [self getThumbnail110PathForFilename: name] atomically:YES]; 

//Thumb150 
UIImage *thumbImage150 = [_imageThumb thumbnailImage:200 transparentBorder:0 cornerRadius:0 interpolationQuality: kCGInterpolationHigh]; 
[UIImageJPEGRepresentation(thumbImage150, 0.75) writeToFile: [self getThumbnail150PathForFilename: name] atomically:YES]; 
NSString *filePath; 
NSString *folderPath = [self.itmesDir stringByAppendingPathComponent: folder]; 
if([[NSUserDefaults standardUserDefaults] boolForKey:FAKE_MODE]){ 
    folderPath = [self.fakeDir stringByAppendingPathComponent: folder]; 
} 
filePath = [folderPath stringByAppendingPathComponent: name]; 


NSCharacterSet *chss = [NSCharacterSet characterSetWithCharactersInString:@"'#%^&{}[]/~|\?.<,"]; 
NSString *dbFilename = [[name componentsSeparatedByCharactersInSet:chss] componentsJoinedByString:@""]; 

if(aDicItem.mediaType == 1){ 
    [UIImageJPEGRepresentation(_imageThumb, 1.0) writeToFile: filePath atomically:YES]; 

    NSLog(@"file path = %@",filePath); 


} 



//Return file path 
return filePath; 
} 

回答

0

使用此代碼

對於那些尋找埃迪在Objective-C的答案。

進口

  • (無效)insertImage:(*的UIImage)圖像intoAlbumNamed:(的NSString *)ALBUMNAME {// 中提取具有標題 「albumNmame」 PHAssetCollection的照片庫的集合* collection = [self fetchAssetCollectionWithAlbumName:albumName];

    如果(集合==無){// 如果我們無法找到一個名爲 「ALBUMNAME」 我們將插入圖像 [PHPhotoLibrary sharedPhotoLibrary] performChanges之前創建它集合:^ { [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle :albumName]; (錯誤!=零)NSLog(@「錯誤插入圖像到相冊中:%@」,error.localizedDescription);}(完成處理程序:^(BOOL成功,NSError * _Nullable錯誤){ } }

    if (success) { 
         //Fetch the newly created collection (which we *assume* exists here) 
         PHAssetCollection *newCollection = [self fetchAssetCollectionWithAlbumName:albumName]; 
         [self insertImage:image intoAssetCollection: newCollection]; 
        } 
    }]; 
    

    }其他{ //如果我們發現標題爲 「ALBUMNAME」 現有AssetCollection,插入它 [自insertImage:圖像intoAssetCollection:收藏] } }

  • (PHAssetCollection *)fetchAssetCollectionWithAlbumName:(的NSString *)ALBUMNAME { PHFetchOptions * FetchOptions的= [PHFetchOptions新]; //提供謂詞以匹配相冊的標題。 fetchOptions.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@「title =='%@'」,albumName]];

    //使用提取選項 PHFetchResult獲取相冊* fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum亞型:PHAssetCollectionSubtypeAlbumRegular選項:類別FetchOptions];

    //假設相冊存在並且沒有相冊共享它的名字,它應該是唯一取得的結果 return fetchResult.firstObject; }

相關問題