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)
|
return [[MTSignal alloc] initWithGenerator:^id<MTDisposable>(MTSubscriber *subscriber)
|
||||||
{
|
{
|
||||||
|
bool preferAsFile = false;
|
||||||
|
#if DEBUG
|
||||||
|
preferAsFile = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
CGSize maxSize = CGSizeMake(1280.0, 1280.0);
|
CGSize maxSize = CGSizeMake(1280.0, 1280.0);
|
||||||
NSDictionary *imageOptions = @{
|
NSDictionary *imageOptions = @{
|
||||||
NSItemProviderPreferredImageSizeKey: [NSValue valueWithCGSize:maxSize]
|
NSItemProviderPreferredImageSizeKey: [NSValue valueWithCGSize:maxSize]
|
||||||
};
|
};
|
||||||
|
if (preferAsFile) {
|
||||||
|
imageOptions = nil;
|
||||||
|
}
|
||||||
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
|
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
|
||||||
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:imageOptions completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
|
[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)]) {
|
if (error != nil && ![(NSObject *)item respondsToSelector:@selector(CGImage)] && ![(NSObject *)item respondsToSelector:@selector(absoluteString)]) {
|
||||||
@ -193,44 +201,63 @@ static CGSize TGFitSize(CGSize size, CGSize maxSize) {
|
|||||||
if ([(NSObject *)item respondsToSelector:@selector(absoluteString)]) {
|
if ([(NSObject *)item respondsToSelector:@selector(absoluteString)]) {
|
||||||
NSURL *url = (NSURL *)item;
|
NSURL *url = (NSURL *)item;
|
||||||
|
|
||||||
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) url, NULL);
|
if (preferAsFile) {
|
||||||
|
NSData *data = [[NSData alloc] initWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:nil];
|
||||||
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
|
if (data == nil) {
|
||||||
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
|
[subscriber putError:nil];
|
||||||
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
|
return;
|
||||||
(id) kCGImageSourceThumbnailMaxPixelSize : @(maxSize.width)
|
}
|
||||||
};
|
NSString *fileName = [[url pathComponents] lastObject];
|
||||||
|
if (fileName.length == 0) {
|
||||||
CGImageRef image = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
|
fileName = @"file.bin";
|
||||||
CFRelease(src);
|
}
|
||||||
|
NSString *extension = [fileName pathExtension];
|
||||||
if (image == nil) {
|
NSString *mimeType = [TGMimeTypeMap mimeTypeForExtension:[extension lowercaseString]];
|
||||||
[subscriber putError:nil];
|
if (mimeType == nil) {
|
||||||
return;
|
mimeType = @"application/octet-stream";
|
||||||
}
|
}
|
||||||
|
[subscriber putNext:@{@"data": data, @"fileName": fileName, @"mimeType": mimeType, @"treatAsFile": @true}];
|
||||||
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSString alloc] initWithFormat:@"img%d", (int)arc4random()]];
|
|
||||||
CFURLRef tempUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:tempPath];
|
|
||||||
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(tempUrl, kUTTypeJPEG, 1, NULL);
|
|
||||||
NSDictionary *properties = @{ (__bridge NSString *)kCGImageDestinationLossyCompressionQuality: @(0.52)};
|
|
||||||
|
|
||||||
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)properties);
|
|
||||||
CGImageDestinationAddImage(destination, image, nil);
|
|
||||||
|
|
||||||
if (!CGImageDestinationFinalize(destination)) {
|
|
||||||
CFRelease(destination);
|
|
||||||
|
|
||||||
[subscriber putError:nil];
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
CFRelease(destination);
|
|
||||||
NSData *resultData = [[NSData alloc] initWithContentsOfFile:tempPath options:NSDataReadingMappedIfSafe error:nil];
|
|
||||||
if (resultData != nil) {
|
|
||||||
[subscriber putNext:@{@"scaledImageData": resultData, @"scaledImageDimensions": [NSValue valueWithCGSize:CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image))]}];
|
|
||||||
[subscriber putCompletion];
|
[subscriber putCompletion];
|
||||||
} else {
|
} else {
|
||||||
[subscriber putError:nil];
|
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) url, NULL);
|
||||||
|
|
||||||
|
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
|
||||||
|
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
|
||||||
|
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
|
||||||
|
(id) kCGImageSourceThumbnailMaxPixelSize : @(maxSize.width)
|
||||||
|
};
|
||||||
|
|
||||||
|
CGImageRef image = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
|
||||||
|
CFRelease(src);
|
||||||
|
|
||||||
|
if (image == nil) {
|
||||||
|
[subscriber putError:nil];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSString alloc] initWithFormat:@"img%d", (int)arc4random()]];
|
||||||
|
CFURLRef tempUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:tempPath];
|
||||||
|
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(tempUrl, kUTTypeJPEG, 1, NULL);
|
||||||
|
NSDictionary *properties = @{ (__bridge NSString *)kCGImageDestinationLossyCompressionQuality: @(0.52)};
|
||||||
|
|
||||||
|
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)properties);
|
||||||
|
CGImageDestinationAddImage(destination, image, nil);
|
||||||
|
|
||||||
|
if (!CGImageDestinationFinalize(destination)) {
|
||||||
|
CFRelease(destination);
|
||||||
|
|
||||||
|
[subscriber putError:nil];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CFRelease(destination);
|
||||||
|
NSData *resultData = [[NSData alloc] initWithContentsOfFile:tempPath options:NSDataReadingMappedIfSafe error:nil];
|
||||||
|
if (resultData != nil) {
|
||||||
|
[subscriber putNext:@{@"scaledImageData": resultData, @"scaledImageDimensions": [NSValue valueWithCGSize:CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image))]}];
|
||||||
|
[subscriber putCompletion];
|
||||||
|
} else {
|
||||||
|
[subscriber putError:nil];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
[subscriber putNext:@{@"image": item}];
|
[subscriber putNext:@{@"image": item}];
|
||||||
|
@ -144,7 +144,12 @@ private func preparedShareItem(account: Account, to peerId: PeerId, value: [Stri
|
|||||||
let fileName = value["fileName"] as? String
|
let fileName = value["fileName"] as? String
|
||||||
let mimeType = (value["mimeType"] as? String) ?? "application/octet-stream"
|
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
|
var isGif = false
|
||||||
if data.count > 4 {
|
if data.count > 4 {
|
||||||
data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Void in
|
data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Void in
|
||||||
|
Loading…
x
Reference in New Issue
Block a user