|
| 1 | +#import <React/RCTBridgeModule.h> |
| 2 | +#import <Photos/Photos.h> |
| 3 | +#import <MobileCoreServices/MobileCoreServices.h> |
| 4 | + |
| 5 | +@interface MediaConverter : NSObject <RCTBridgeModule> |
| 6 | +@end |
| 7 | + |
| 8 | +@implementation MediaConverter |
| 9 | + |
| 10 | +RCT_EXPORT_MODULE(); |
| 11 | + |
| 12 | +RCT_EXPORT_METHOD(convertMedia:(NSString *)uri |
| 13 | + resolver:(RCTPromiseResolveBlock)resolve |
| 14 | + rejecter:(RCTPromiseRejectBlock)reject) |
| 15 | +{ |
| 16 | + if ([uri hasPrefix:@"ph://"]) { |
| 17 | + NSString *assetID = [uri stringByReplacingOccurrencesOfString:@"ph://" withString:@""]; |
| 18 | + PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetID] options:nil]; |
| 19 | + |
| 20 | + if (result.count == 0) { |
| 21 | + reject(@"E_PHOTO_NOT_FOUND", @"Photo not found", nil); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + PHAsset *asset = result.firstObject; |
| 26 | + |
| 27 | + if (asset.mediaType == PHAssetMediaTypeImage) { |
| 28 | + // Handle image conversion |
| 29 | + [self convertImageAsset:asset resolver:resolve rejecter:reject]; |
| 30 | + } else if (asset.mediaType == PHAssetMediaTypeVideo) { |
| 31 | + // Handle video conversion |
| 32 | + [self convertVideoAsset:asset resolver:resolve rejecter:reject]; |
| 33 | + } else { |
| 34 | + reject(@"E_UNSUPPORTED_MEDIA_TYPE", @"Unsupported media type", nil); |
| 35 | + } |
| 36 | + } else { |
| 37 | + reject(@"E_INVALID_URI", @"Invalid URI format", nil); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +- (void)convertImageAsset:(PHAsset *)asset |
| 42 | + resolver:(RCTPromiseResolveBlock)resolve |
| 43 | + rejecter:(RCTPromiseRejectBlock)reject |
| 44 | +{ |
| 45 | + PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; |
| 46 | + options.synchronous = YES; |
| 47 | + options.networkAccessAllowed = YES; |
| 48 | + |
| 49 | + [[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, CGImagePropertyOrientation orientation, NSDictionary * _Nullable info) { |
| 50 | + if (imageData) { |
| 51 | + NSString *fileExtension = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)dataUTI, kUTTagClassFilenameExtension); |
| 52 | + NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [[NSUUID UUID] UUIDString], fileExtension]]; |
| 53 | + |
| 54 | + NSError *error = nil; |
| 55 | + [imageData writeToFile:filePath options:NSDataWritingAtomic error:&error]; |
| 56 | + |
| 57 | + if (error) { |
| 58 | + reject(@"E_FILE_SAVE_FAILED", @"Failed to save image file", error); |
| 59 | + } else { |
| 60 | + resolve(filePath); |
| 61 | + } |
| 62 | + } else { |
| 63 | + reject(@"E_IMAGE_REQUEST_FAILED", @"Failed to get image data", nil); |
| 64 | + } |
| 65 | + }]; |
| 66 | +} |
| 67 | + |
| 68 | +- (void)convertVideoAsset:(PHAsset *)asset |
| 69 | + resolver:(RCTPromiseResolveBlock)resolve |
| 70 | + rejecter:(RCTPromiseRejectBlock)reject |
| 71 | +{ |
| 72 | + PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init]; |
| 73 | + options.networkAccessAllowed = YES; |
| 74 | + |
| 75 | + [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) { |
| 76 | + if ([avAsset isKindOfClass:[AVURLAsset class]]) { |
| 77 | + AVURLAsset *urlAsset = (AVURLAsset *)avAsset; |
| 78 | + NSURL *url = urlAsset.URL; |
| 79 | + NSString *fileExtension = [url pathExtension]; |
| 80 | + |
| 81 | + NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [[NSUUID UUID] UUIDString], fileExtension]]; |
| 82 | + NSError *error = nil; |
| 83 | + [[NSFileManager defaultManager] copyItemAtURL:url toURL:[NSURL fileURLWithPath:filePath] error:&error]; |
| 84 | + |
| 85 | + if (error) { |
| 86 | + reject(@"E_VIDEO_SAVE_FAILED", @"Failed to save video file", error); |
| 87 | + } else { |
| 88 | + resolve(filePath); |
| 89 | + } |
| 90 | + } else { |
| 91 | + reject(@"E_VIDEO_REQUEST_FAILED", @"Failed to get video data", nil); |
| 92 | + } |
| 93 | + }]; |
| 94 | +} |
| 95 | + |
| 96 | +@end |
0 commit comments