mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Experimental treat image as file
This commit is contained in:
parent
5ba59faa78
commit
792dcac27b
@ -172,10 +172,18 @@ static CGSize TGFitSize(CGSize size, CGSize maxSize) {
|
||||
{
|
||||
return [[MTSignal alloc] initWithGenerator:^id<MTDisposable>(MTSubscriber *subscriber)
|
||||
{
|
||||
bool preferAsFile = false;
|
||||
#if DEBUG
|
||||
preferAsFile = true;
|
||||
#endif
|
||||
|
||||
CGSize maxSize = CGSizeMake(1280.0, 1280.0);
|
||||
NSDictionary *imageOptions = @{
|
||||
NSItemProviderPreferredImageSizeKey: [NSValue valueWithCGSize:maxSize]
|
||||
};
|
||||
if (preferAsFile) {
|
||||
imageOptions = nil;
|
||||
}
|
||||
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
|
||||
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:imageOptions completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
|
||||
if (error != nil && ![(NSObject *)item respondsToSelector:@selector(CGImage)] && ![(NSObject *)item respondsToSelector:@selector(absoluteString)]) {
|
||||
@ -193,6 +201,24 @@ static CGSize TGFitSize(CGSize size, CGSize maxSize) {
|
||||
if ([(NSObject *)item respondsToSelector:@selector(absoluteString)]) {
|
||||
NSURL *url = (NSURL *)item;
|
||||
|
||||
if (preferAsFile) {
|
||||
NSData *data = [[NSData alloc] initWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:nil];
|
||||
if (data == nil) {
|
||||
[subscriber putError:nil];
|
||||
return;
|
||||
}
|
||||
NSString *fileName = [[url pathComponents] lastObject];
|
||||
if (fileName.length == 0) {
|
||||
fileName = @"file.bin";
|
||||
}
|
||||
NSString *extension = [fileName pathExtension];
|
||||
NSString *mimeType = [TGMimeTypeMap mimeTypeForExtension:[extension lowercaseString]];
|
||||
if (mimeType == nil) {
|
||||
mimeType = @"application/octet-stream";
|
||||
}
|
||||
[subscriber putNext:@{@"data": data, @"fileName": fileName, @"mimeType": mimeType, @"treatAsFile": @true}];
|
||||
[subscriber putCompletion];
|
||||
} else {
|
||||
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) url, NULL);
|
||||
|
||||
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
|
||||
@ -232,6 +258,7 @@ static CGSize TGFitSize(CGSize size, CGSize maxSize) {
|
||||
} else {
|
||||
[subscriber putError:nil];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
[subscriber putNext:@{@"image": item}];
|
||||
[subscriber putCompletion];
|
||||
|
@ -144,7 +144,12 @@ private func preparedShareItem(account: Account, to peerId: PeerId, value: [Stri
|
||||
let fileName = value["fileName"] as? String
|
||||
let mimeType = (value["mimeType"] as? String) ?? "application/octet-stream"
|
||||
|
||||
if let image = UIImage(data: data) {
|
||||
var treatAsFile = false
|
||||
if let boolValue = value["treatAsFile"] as? Bool, boolValue {
|
||||
treatAsFile = true
|
||||
}
|
||||
|
||||
if !treatAsFile, let image = UIImage(data: data) {
|
||||
var isGif = false
|
||||
if data.count > 4 {
|
||||
data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Void in
|
||||
|
Loading…
x
Reference in New Issue
Block a user