mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Share Extension: Use different image scaling approach to avoid memory-limit-exceeded crashes
This commit is contained in:
parent
8f070557fa
commit
ee68b7b539
@ -198,16 +198,43 @@ static CGSize TGFitSize(CGSize size, CGSize maxSize) {
|
|||||||
} else {
|
} else {
|
||||||
if ([(NSObject *)item respondsToSelector:@selector(absoluteString)]) {
|
if ([(NSObject *)item respondsToSelector:@selector(absoluteString)]) {
|
||||||
NSURL *url = (NSURL *)item;
|
NSURL *url = (NSURL *)item;
|
||||||
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[url path]];
|
|
||||||
if (image != nil) {
|
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) url, NULL);
|
||||||
UIImage *result = TGScaleImageToPixelSize(image, TGFitSize(image.size, maxSize));
|
|
||||||
NSData *resultData = UIImageJPEGRepresentation(result, 0.52f);
|
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
|
||||||
if (resultData != nil) {
|
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
|
||||||
[subscriber putNext:@{@"scaledImageData": resultData, @"scaledImageDimensions": [NSValue valueWithCGSize:result.size]}];
|
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
|
||||||
[subscriber putCompletion];
|
(id) kCGImageSourceThumbnailMaxPixelSize : @(maxSize.width)
|
||||||
} else {
|
};
|
||||||
[subscriber putError:nil];
|
|
||||||
}
|
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 {
|
} else {
|
||||||
[subscriber putError:nil];
|
[subscriber putError:nil];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user