Nil*_*ani 5 iphone photo-gallery ios react-native
目前我正在获取内存问题因为我在React Native的Flatlist中加载直接图像.问题是由于高分辨率图像达到内存限制并且应用程序在iPhone上崩溃.有什么方法我可以像图片网址一样获取直接的拇指网址(例如url:'assets-library://asset/asset.JPG?id = 5BECA80C-33B3-46A0-AE44-CF28A838CECF&ext = JPG',)?
目前我正在使用"React-native-photo-framework".
最后我对这个问题有了自己的解决方案。我已经尝试了所有解决方法,但无法解决。最后我知道新的照片库提供了获取调整大小的图像的选项,但这就是不适用于react-native-photos-framework。我的本地经验将帮助我解决我的问题,希望这对你有帮助。这是包含详细说明和代码的链接。
让我在这里添加代码片段。
导入原生照片库
#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];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
622 次 |
| 最近记录: |