当我尝试将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;
}
最佳答案
使用此代码
对于那些在Objective-C中寻找Eddy答案的人。
进口
//在照片库中获取标题为“albumNmame”的照片集
PHAssetCollection * collection = [self fetchAssetCollectionWithAlbumName:albumName];
if(collection == nil){
//如果我们找不到名为“albumName”的集合,则会在插入图像之前创建它
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^ {
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
} completeHandler:^(BOOL成功,NSError * _Nullable错误){
如果(错误!=无){
NSLog(@“将图片插入相册时出错:%@”,error.localizedDescription);
}
if (success) {
//Fetch the newly created collection (which we *assume* exists here)
PHAssetCollection *newCollection = [self fetchAssetCollectionWithAlbumName:albumName];
[self insertImage:image intoAssetCollection: newCollection];
}
}];
}其他{
//如果找到标题为“albumName”的现有AssetCollection,则将其插入其中
[self insertImage:image intoAssetCollection:collection];
}
PHFetchOptions * fetchOptions = [PHFetchOptions new];
//提供谓词以匹配专辑的标题。
fetchOptions.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@“title =='%@'”,albumName]];
//使用获取选项获取相册
PHFetchResult * fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum子类型:PHAssetCollectionSubtypeAlbum常规选项:fetchOptions];
//假设相册存在且没有相册共享其名称,则应该是获取的唯一结果
返回fetchResult.firstObject;
关于ios - 由于内存问题,将多张图像从相机胶卷保存到文档目录时崩溃了,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46701383/