简体   繁体   English

如何从Photolibrary图像获取拇指图像?

[英]How to get thumb image from Photolibrary image?

Currently i am fetching memory issue because i am loading direct image in Flatlist of React Native. 目前我正在获取内存问题因为我在React Native的Flatlist中加载直接图像。 Issue is that due to high resolutions images memory limit reached and app get crashed on iPhone. 问题是由于高分辨率图像达到内存限制并且应用程序在iPhone上崩溃。 Is there any way i can fetch direct thumb url like image url (eg url: 'assets-library://asset/asset.JPG?id=5BECA80C-33B3-46A0-AE44-CF28A838CECF&ext=JPG',) ? 有什么方法我可以像图片网址一样获取直接的拇指网址(例如url:'assets-library://asset/asset.JPG?id = 5BECA80C-33B3-46A0-AE44-CF28A838CECF&ext = JPG',)?

Currently i am using 'React-native-photo-framework'. 目前我正在使用“React-native-photo-framework”。

getAssets takes a prepareForSizeDisplay prop. getAssets采用prepareForSizeDisplay道具。 This uses PHCachingImageManager to request images of assets at a specified size. 这使用PHCachingImageManager来请求指定大小的资产的图像。

Example: 例:

RNPhotosFramework.getAssets({
    startIndex: 0,
    endIndex: 100,
    prepareForSizeDisplay: Rect(100,100),
    fetchOptions: {
        sourceTypes: ['userLibrary'],
        sortDescriptors: [{
            key: 'creationDate',
            ascending: true,
        }]
    }
}).then((response) => console.log(response.assets));

When the user taps the row in your FlatList , you can then fetch the full-size asset. 当用户点击FlatList中的FlatList ,您可以获取完整大小的资产。 Don't fetch the full-size asset until you need to display it. 在您需要显示之前,请不要获取完整大小的资源。

Finally i have my own solution for this question. 最后,我有自己的解决方案来解决这个问题。 I have tried all ways to resolve but i am not able to resolve it. 我已经尝试了所有方法来解决,但我无法解决它。 Lastly i come to know that new Photo Library provide options to fetch resized image but thats what not working with react-native-photos-framework . 最后,我开始知道新的Photo Library提供了获取已调整大小的图像的选项,但那不适用于react-native-photos-framework My native experience will help me to resolve my issue, hope this will help you. 我的本土经验将帮助我解决我的问题,希望这会对你有所帮助。 Here is link with detail description and code. 是与详细描述和代码的链接。

Let me add here code snippets. 我在这里添加代码片段。

Import Native Photo Library 导入本机照片库

#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];

}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM