当我尝试将超过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 :(得分:0)
使用此代码
对于那些在Objective-C中寻找Eddy答案的人。
(void)insertImage:(UIImage *)image intoAlbumNamed:(NSString *)albumName { //在照片库中获取标题为&#34; albumNmame&#34; PHAssetCollection * collection = [self fetchAssetCollectionWithAlbumName:albumName];
if(collection == nil){ //如果我们找不到名为&#34; albumName&#34;我们会在插入图片之前创建它 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^ { [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName]; } completionHandler:^(BOOL成功,NSError * _Nullable错误){ if(错误!=无){ NSLog(@&#34;将图像插入相册时出错:%@&#34;,error.localizedDescription); }
if (success) {
//Fetch the newly created collection (which we *assume* exists here)
PHAssetCollection *newCollection = [self fetchAssetCollectionWithAlbumName:albumName];
[self insertImage:image intoAssetCollection: newCollection];
}
}];
} else { //如果我们找到标题为&#34; albumName&#34;的现有AssetCollection,请插入其中 [self insertImage:image intoAssetCollection:collection]; } }
(PHAssetCollection *)fetchAssetCollectionWithAlbumName:(NSString *)albumName { PHFetchOptions * fetchOptions = [PHFetchOptions new]; //提供谓词以匹配专辑的标题。 fetchOptions.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@&#34; title ==&#39;%@&#39;&#34;,albumName]];
//使用fetch选项获取相册 PHFetchResult * fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];
//假设相册存在且没有相册分享它的名称,它应该是唯一获取的结果 return fetchResult.firstObject; }