如何从 Photolibrary 图像中获取缩略图?
How to get thumb image from Photolibrary image?
目前我正在获取内存问题,因为我在 React Native 的 Flatlist 中加载直接图像。问题是由于高分辨率图像达到内存限制,应用程序在 iPhone 上崩溃。有什么方法可以获取像图像 url 那样的直接拇指 url(例如 url:'assets-library://asset/asset.JPG?id=5BECA80C-33B3-46A0-AE44-CF28A838CECF&ext=JPG')?
目前我正在使用'React-native-photo-framework'。
getAssets
需要一个 prepareForSizeDisplay
道具。这使用 PHCachingImageManager
请求指定大小的资产图像。
示例:
RNPhotosFramework.getAssets({
startIndex: 0,
endIndex: 100,
prepareForSizeDisplay: Rect(100,100),
fetchOptions: {
sourceTypes: ['userLibrary'],
sortDescriptors: [{
key: 'creationDate',
ascending: true,
}]
}
}).then((response) => console.log(response.assets));
当用户点击您的 FlatList
中的行时,您就可以获取 full-size 资产。在需要显示之前不要获取 full-size 资产。
这个问题终于有了自己的解决方案。我已经尝试了所有方法来解决,但我无法解决它。最后,我了解到新的照片库提供了获取调整大小后的图像的选项,但是 react-native-photos-framework. My native experience will help me to resolve my issue, hope this will help you. Here 无法使用的是 link,其中包含详细说明和代码。
让我在这里添加代码片段。
导入原生照片库
#import <Photos/Photos.h>
CGSize retinaSquare = CGSizeMake(600, 600);
PHImageRequestOptions *cropToSquare = [[PHImageRequestOptions alloc] init];
cropToSquare.resizeMode = PHImageRequestOptionsResizeModeExact;
cropToSquare.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
[cropToSquare setSynchronous:YES];
NSURL *imageurl = [NSURL URLWithString:@"youimagepath"];
PHFetchResult* asset =[PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObjects:imageurl, nil] options:nil];
[[PHImageManager defaultManager] requestImageForAsset:(PHAsset *)[asset objectAtIndex:0] targetSize:retinaSquare contentMode:PHImageContentModeAspectFit options:cropToSquare resultHandler:^(UIImage *fetchedImage, NSDictionary *info) {
NSData *imageData = UIImageJPEGRepresentation(fetchedImage,0.65);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%0.0f.jpg", timeStamp*1000]];
NSError *error = nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&error];
NSURL* fileUrl = [NSURL fileURLWithPath:filePath];
if(error){
fileUrl = imageurl;
}
NSString *resizedImagePath = [NSString stringWithFormat:@"%@",fileUrl];
}];
目前我正在获取内存问题,因为我在 React Native 的 Flatlist 中加载直接图像。问题是由于高分辨率图像达到内存限制,应用程序在 iPhone 上崩溃。有什么方法可以获取像图像 url 那样的直接拇指 url(例如 url:'assets-library://asset/asset.JPG?id=5BECA80C-33B3-46A0-AE44-CF28A838CECF&ext=JPG')?
目前我正在使用'React-native-photo-framework'。
getAssets
需要一个 prepareForSizeDisplay
道具。这使用 PHCachingImageManager
请求指定大小的资产图像。
示例:
RNPhotosFramework.getAssets({
startIndex: 0,
endIndex: 100,
prepareForSizeDisplay: Rect(100,100),
fetchOptions: {
sourceTypes: ['userLibrary'],
sortDescriptors: [{
key: 'creationDate',
ascending: true,
}]
}
}).then((response) => console.log(response.assets));
当用户点击您的 FlatList
中的行时,您就可以获取 full-size 资产。在需要显示之前不要获取 full-size 资产。
这个问题终于有了自己的解决方案。我已经尝试了所有方法来解决,但我无法解决它。最后,我了解到新的照片库提供了获取调整大小后的图像的选项,但是 react-native-photos-framework. My native experience will help me to resolve my issue, hope this will help you. Here 无法使用的是 link,其中包含详细说明和代码。
让我在这里添加代码片段。
导入原生照片库
#import <Photos/Photos.h>
CGSize retinaSquare = CGSizeMake(600, 600);
PHImageRequestOptions *cropToSquare = [[PHImageRequestOptions alloc] init];
cropToSquare.resizeMode = PHImageRequestOptionsResizeModeExact;
cropToSquare.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
[cropToSquare setSynchronous:YES];
NSURL *imageurl = [NSURL URLWithString:@"youimagepath"];
PHFetchResult* asset =[PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObjects:imageurl, nil] options:nil];
[[PHImageManager defaultManager] requestImageForAsset:(PHAsset *)[asset objectAtIndex:0] targetSize:retinaSquare contentMode:PHImageContentModeAspectFit options:cropToSquare resultHandler:^(UIImage *fetchedImage, NSDictionary *info) {
NSData *imageData = UIImageJPEGRepresentation(fetchedImage,0.65);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%0.0f.jpg", timeStamp*1000]];
NSError *error = nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&error];
NSURL* fileUrl = [NSURL fileURLWithPath:filePath];
if(error){
fileUrl = imageurl;
}
NSString *resizedImagePath = [NSString stringWithFormat:@"%@",fileUrl];
}];