Lazy load attachments in messages view, etc.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 4a33662515
commit 6704396998

@ -10,4 +10,8 @@
- (CGSize)ows_adjustBubbleSize:(CGSize)bubbleSize forImage:(UIImage *)image; - (CGSize)ows_adjustBubbleSize:(CGSize)bubbleSize forImage:(UIImage *)image;
- (CGSize)ows_adjustBubbleSize:(CGSize)bubbleSize forImageSize:(CGSize)imageSize;
- (CGSize)sizeOfImageAtURL:(NSURL *_Nullable)imageURL;
@end @end

@ -3,8 +3,9 @@
// //
#import "JSQMediaItem+OWS.h" #import "JSQMediaItem+OWS.h"
#import "UIDevice+TSHardwareVersion.h"
#import "NumberUtil.h" #import "NumberUtil.h"
#import "UIDevice+TSHardwareVersion.h"
#import <ImageIO/ImageIO.h>
@implementation JSQMediaItem (OWS) @implementation JSQMediaItem (OWS)
@ -15,7 +16,12 @@
} }
- (CGSize)ows_adjustBubbleSize:(CGSize)bubbleSize forImage:(UIImage *)image { - (CGSize)ows_adjustBubbleSize:(CGSize)bubbleSize forImage:(UIImage *)image {
double aspectRatio = image.size.height / image.size.width; return [self ows_adjustBubbleSize:bubbleSize forImageSize:image.size];
}
- (CGSize)ows_adjustBubbleSize:(CGSize)bubbleSize forImageSize:(CGSize)imageSize
{
double aspectRatio = imageSize.height / imageSize.width;
double clampedAspectRatio = [NumberUtil clamp:aspectRatio toMin:0.5 andMax:1.5]; double clampedAspectRatio = [NumberUtil clamp:aspectRatio toMin:0.5 andMax:1.5];
if ([[UIDevice currentDevice] isiPhoneVersionSixOrMore]) { if ([[UIDevice currentDevice] isiPhoneVersionSixOrMore]) {
@ -32,4 +38,33 @@
return bubbleSize; return bubbleSize;
} }
- (CGSize)sizeOfImageAtURL:(NSURL *_Nullable)imageURL
{
if (!imageURL) {
return CGSizeZero;
}
// With CGImageSource we avoid loading the whole image into memory.
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)imageURL, NULL);
if (!source) {
return CGSizeZero;
}
NSDictionary *options = @{
(NSString *)kCGImageSourceShouldCache : @(NO),
};
NSDictionary *properties
= (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, 0, (CFDictionaryRef)options);
CGSize imageSize = CGSizeZero;
if (properties) {
NSNumber *width = properties[(NSString *)kCGImagePropertyPixelWidth];
NSNumber *height = properties[(NSString *)kCGImagePropertyPixelHeight];
if (width && height) {
imageSize = CGSizeMake(width.floatValue, height.floatValue);
}
}
CFRelease(source);
return imageSize;
}
@end @end

@ -12,10 +12,9 @@ NS_ASSUME_NONNULL_BEGIN
@interface TSAnimatedAdapter : JSQMediaItem <OWSMessageEditing, OWSMessageMediaAdapter> @interface TSAnimatedAdapter : JSQMediaItem <OWSMessageEditing, OWSMessageMediaAdapter>
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachment incoming:(BOOL)incoming; @property (nonatomic) NSString *attachmentId;
@property NSString *attachmentId; - (instancetype)initWithAttachment:(TSAttachmentStream *)attachment incoming:(BOOL)incoming;
@property NSData *fileData;
@end @end

@ -15,13 +15,12 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface TSAnimatedAdapter () @interface TSAnimatedAdapter ()
//<FLAnimatedImageViewDebugDelegate>
@property (nonatomic, nullable) FLAnimatedImageView *cachedImageView; @property (nonatomic, nullable) FLAnimatedImageView *cachedImageView;
@property (nonatomic) UIImage *image;
@property (nonatomic) TSAttachmentStream *attachment; @property (nonatomic) TSAttachmentStream *attachment;
@property (nonatomic, nullable) AttachmentUploadView *attachmentUploadView; @property (nonatomic, nullable) AttachmentUploadView *attachmentUploadView;
@property (nonatomic) BOOL incoming; @property (nonatomic) BOOL incoming;
@property (nonatomic) CGSize imageSize;
// See comments on OWSMessageMediaAdapter. // See comments on OWSMessageMediaAdapter.
@property (nonatomic, nullable, weak) id lastPresentingCell; @property (nonatomic, nullable, weak) id lastPresentingCell;
@ -40,9 +39,8 @@ NS_ASSUME_NONNULL_BEGIN
_cachedImageView = nil; _cachedImageView = nil;
_attachment = attachment; _attachment = attachment;
_attachmentId = attachment.uniqueId; _attachmentId = attachment.uniqueId;
_image = [attachment image];
_fileData = [NSData dataWithContentsOfURL:[attachment mediaURL]];
_incoming = incoming; _incoming = incoming;
_imageSize = [self sizeOfImageAtURL:attachment.mediaURL];
} }
return self; return self;
@ -50,6 +48,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)clearAllViews - (void)clearAllViews
{ {
OWSAssert([NSThread isMainThread]);
[_cachedImageView removeFromSuperview]; [_cachedImageView removeFromSuperview];
_cachedImageView = nil; _cachedImageView = nil;
_attachmentUploadView = nil; _attachmentUploadView = nil;
@ -69,7 +69,7 @@ NS_ASSUME_NONNULL_BEGIN
- (NSUInteger)hash - (NSUInteger)hash
{ {
return super.hash ^ self.image.hash; return super.hash ^ self.attachment.uniqueId.hash;
} }
#pragma mark - OWSMessageMediaAdapter #pragma mark - OWSMessageMediaAdapter
@ -95,9 +95,12 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - JSQMessageMediaData protocol #pragma mark - JSQMessageMediaData protocol
- (UIView *)mediaView { - (UIView *)mediaView {
OWSAssert([NSThread isMainThread]);
if (self.cachedImageView == nil) { if (self.cachedImageView == nil) {
// Use Flipboard FLAnimatedImage library to display gifs // Use Flipboard FLAnimatedImage library to display gifs
FLAnimatedImage *animatedGif = [FLAnimatedImage animatedImageWithGIFData:self.fileData]; NSData *fileData = [NSData dataWithContentsOfURL:[self.attachment mediaURL]];
FLAnimatedImage *animatedGif = [FLAnimatedImage animatedImageWithGIFData:fileData];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init]; FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = animatedGif; imageView.animatedImage = animatedGif;
CGSize size = [self mediaViewDisplaySize]; CGSize size = [self mediaViewDisplaySize];
@ -119,7 +122,7 @@ NS_ASSUME_NONNULL_BEGIN
} }
- (CGSize)mediaViewDisplaySize { - (CGSize)mediaViewDisplaySize {
return [self ows_adjustBubbleSize:[super mediaViewDisplaySize] forImage:self.image]; return [self ows_adjustBubbleSize:[super mediaViewDisplaySize] forImageSize:self.imageSize];
} }
#pragma mark - OWSMessageEditing Protocol #pragma mark - OWSMessageEditing Protocol
@ -139,11 +142,12 @@ NS_ASSUME_NONNULL_BEGIN
} }
UIPasteboard *pasteboard = UIPasteboard.generalPasteboard; UIPasteboard *pasteboard = UIPasteboard.generalPasteboard;
[pasteboard setData:self.fileData forPasteboardType:utiType]; NSData *data = [NSData dataWithContentsOfURL:[self.attachment mediaURL]];
[pasteboard setData:data forPasteboardType:utiType];
} else if (action == NSSelectorFromString(@"save:")) { } else if (action == NSSelectorFromString(@"save:")) {
NSData *photoData = self.fileData; NSData *data = [NSData dataWithContentsOfURL:[self.attachment mediaURL]];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:photoData [library writeImageDataToSavedPhotosAlbum:data
metadata:nil metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) { completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) { if (error) {

@ -6,6 +6,7 @@
#import "AttachmentUploadView.h" #import "AttachmentUploadView.h"
#import "JSQMediaItem+OWS.h" #import "JSQMediaItem+OWS.h"
#import "TSAttachmentStream.h" #import "TSAttachmentStream.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <JSQMessagesViewController/JSQMessagesMediaViewBubbleImageMasker.h> #import <JSQMessagesViewController/JSQMessagesMediaViewBubbleImageMasker.h>
#import <MobileCoreServices/MobileCoreServices.h> #import <MobileCoreServices/MobileCoreServices.h>
#import <SignalServiceKit/MimeTypeUtil.h> #import <SignalServiceKit/MimeTypeUtil.h>
@ -17,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, nullable) UIImageView *cachedImageView; @property (nonatomic, nullable) UIImageView *cachedImageView;
@property (nonatomic, nullable) AttachmentUploadView *attachmentUploadView; @property (nonatomic, nullable) AttachmentUploadView *attachmentUploadView;
@property (nonatomic) BOOL incoming; @property (nonatomic) BOOL incoming;
@property (nonatomic) CGSize imageSize;
// See comments on OWSMessageMediaAdapter. // See comments on OWSMessageMediaAdapter.
@property (nonatomic, nullable, weak) id lastPresentingCell; @property (nonatomic, nullable, weak) id lastPresentingCell;
@ -29,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachment incoming:(BOOL)incoming - (instancetype)initWithAttachment:(TSAttachmentStream *)attachment incoming:(BOOL)incoming
{ {
self = [super initWithImage:attachment.image]; self = [super initWithImage:nil];
if (!self) { if (!self) {
return self; return self;
@ -39,12 +41,15 @@ NS_ASSUME_NONNULL_BEGIN
_attachment = attachment; _attachment = attachment;
_attachmentId = attachment.uniqueId; _attachmentId = attachment.uniqueId;
_incoming = incoming; _incoming = incoming;
_imageSize = [self sizeOfImageAtURL:attachment.mediaURL];
return self; return self;
} }
- (void)clearAllViews - (void)clearAllViews
{ {
OWSAssert([NSThread isMainThread]);
[_cachedImageView removeFromSuperview]; [_cachedImageView removeFromSuperview];
_cachedImageView = nil; _cachedImageView = nil;
_attachmentUploadView = nil; _attachmentUploadView = nil;
@ -64,13 +69,15 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - JSQMessageMediaData protocol #pragma mark - JSQMessageMediaData protocol
- (UIView *)mediaView { - (UIView *)mediaView {
if (self.image == nil) { OWSAssert([NSThread isMainThread]);
return nil;
}
if (self.cachedImageView == nil) { if (self.cachedImageView == nil) {
UIImage *image = self.attachment.image;
if (!image) {
return nil;
}
CGSize size = [self mediaViewDisplaySize]; CGSize size = [self mediaViewDisplaySize];
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.image]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.frame = CGRectMake(0.0f, 0.0f, size.width, size.height); imageView.frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
imageView.clipsToBounds = YES; imageView.clipsToBounds = YES;
@ -93,7 +100,7 @@ NS_ASSUME_NONNULL_BEGIN
} }
- (CGSize)mediaViewDisplaySize { - (CGSize)mediaViewDisplaySize {
return [self ows_adjustBubbleSize:[super mediaViewDisplaySize] forImage:self.image]; return [self ows_adjustBubbleSize:[super mediaViewDisplaySize] forImageSize:self.imageSize];
} }
#pragma mark - OWSMessageEditing Protocol #pragma mark - OWSMessageEditing Protocol
@ -106,14 +113,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)performEditingAction:(SEL)action - (void)performEditingAction:(SEL)action
{ {
NSString *actionString = NSStringFromSelector(action); NSString *actionString = NSStringFromSelector(action);
if (!self.image) {
OWSAssert(NO);
DDLogWarn(@"Refusing to perform '%@' action with nil image for %@: attachmentId=%@. (corrupted attachment?)",
actionString,
self.class,
self.attachmentId);
return;
}
if (action == @selector(copy:)) { if (action == @selector(copy:)) {
// We should always copy to the pasteboard as data, not an UIImage. // We should always copy to the pasteboard as data, not an UIImage.
@ -129,7 +128,15 @@ NS_ASSUME_NONNULL_BEGIN
[UIPasteboard.generalPasteboard setData:data forPasteboardType:utiType]; [UIPasteboard.generalPasteboard setData:data forPasteboardType:utiType];
return; return;
} else if (action == NSSelectorFromString(@"save:")) { } else if (action == NSSelectorFromString(@"save:")) {
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil); NSData *data = [NSData dataWithContentsOfURL:[self.attachment mediaURL]];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:data
metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
DDLogWarn(@"Error Saving image to photo album: %@", error);
}
}];
return; return;
} }

@ -320,9 +320,9 @@ NS_ASSUME_NONNULL_BEGIN
[bottomLabel sizeToFit]; [bottomLabel sizeToFit];
[mediaView addSubview:bottomLabel]; [mediaView addSubview:bottomLabel];
const CGFloat topLabelHeight = ceil(topLabel.font.lineHeight); const CGFloat topLabelHeight = (CGFloat)ceil(topLabel.font.lineHeight);
const CGFloat kAudioProgressViewHeight = 12.f; const CGFloat kAudioProgressViewHeight = 12.f;
const CGFloat bottomLabelHeight = ceil(bottomLabel.font.lineHeight); const CGFloat bottomLabelHeight = (CGFloat)ceil(bottomLabel.font.lineHeight);
CGRect labelsBounds = CGRectZero; CGRect labelsBounds = CGRectZero;
labelsBounds.origin.x = (CGFloat)round(iconFrame.origin.x + iconFrame.size.width + kLabelHSpacing); labelsBounds.origin.x = (CGFloat)round(iconFrame.origin.x + iconFrame.size.width + kLabelHSpacing);
labelsBounds.size.width = contentFrame.origin.x + contentFrame.size.width - labelsBounds.origin.x; labelsBounds.size.width = contentFrame.origin.x + contentFrame.size.width - labelsBounds.origin.x;

Loading…
Cancel
Save