Use new thumbnails in media gallery thumbnails.

pull/1/head
Matthew Chen 7 years ago
parent ac4365e1c9
commit 3437361d70

@ -33,13 +33,9 @@ public struct MediaGalleryItem: Equatable, Hashable {
return attachmentStream.isImage() return attachmentStream.isImage()
} }
var thumbnailImage: UIImage { public typealias AsyncThumbnailBlock = (UIImage) -> Void
guard let image = attachmentStream.thumbnailImage() else { func thumbnailImage(async:@escaping AsyncThumbnailBlock) -> UIImage? {
owsFail("\(logTag) in \(#function) unexpectedly unable to build attachment thumbnail") return attachmentStream.thumbnailImageSmall(completion: async)
return UIImage()
}
return image
} }
var fullSizedImage: UIImage { var fullSizedImage: UIImage {

@ -922,7 +922,24 @@ private class MediaGalleryCell: UICollectionViewCell {
public func configure(item: MediaGalleryItem) { public func configure(item: MediaGalleryItem) {
self.item = item self.item = item
self.imageView.image = item.thumbnailImage if let image = item.thumbnailImage(async: {
[weak self] (image) in
guard let strongSelf = self else {
return
}
guard strongSelf.item == item else {
return
}
strongSelf.imageView.image = image
strongSelf.imageView.backgroundColor = UIColor.clear
}) {
self.imageView.image = image
self.imageView.backgroundColor = UIColor.clear
} else {
// TODO: Show a placeholder?
self.imageView.backgroundColor = Theme.offBackgroundColor
}
if item.isVideo { if item.isVideo {
self.contentTypeBadgeView.isHidden = false self.contentTypeBadgeView.isHidden = false
self.contentTypeBadgeView.image = MediaGalleryCell.videoBadgeImage self.contentTypeBadgeView.image = MediaGalleryCell.videoBadgeImage

@ -323,7 +323,7 @@ typedef void (^OrphanDataBlock)(OWSOrphanData *);
OWSFail(@"%@ attachment has no file path.", self.logTag); OWSFail(@"%@ attachment has no file path.", self.logTag);
} }
NSString *_Nullable thumbnailPath = [attachmentStream thumbnailPath]; NSString *_Nullable thumbnailPath = [attachmentStream legacyThumbnailPath];
if (thumbnailPath.length > 0) { if (thumbnailPath.length > 0) {
[allAttachmentFilePaths addObject:thumbnailPath]; [allAttachmentFilePaths addObject:thumbnailPath];
} }

@ -227,7 +227,7 @@ NS_ASSUME_NONNULL_BEGIN
authorId:authorId authorId:authorId
body:quotedText body:quotedText
bodySource:TSQuotedMessageContentSourceLocal bodySource:TSQuotedMessageContentSourceLocal
thumbnailImage:quotedAttachment.thumbnailImage thumbnailImage:quotedAttachment.legacyThumbnailImage
contentType:quotedAttachment.contentType contentType:quotedAttachment.contentType
sourceFilename:quotedAttachment.sourceFilename sourceFilename:quotedAttachment.sourceFilename
attachmentStream:quotedAttachment attachmentStream:quotedAttachment

@ -142,17 +142,17 @@ private struct OWSThumbnailRequest {
var thumbnailSize = CGSize.zero var thumbnailSize = CGSize.zero
if originalSize.width > originalSize.height { if originalSize.width > originalSize.height {
thumbnailSize.width = CGFloat(thumbnailRequest.thumbnailDimensionPoints) thumbnailSize.width = CGFloat(thumbnailRequest.thumbnailDimensionPoints)
thumbnailSize.height = round(CGFloat(thumbnailRequest.thumbnailDimensionPoints) * thumbnailSize.height / thumbnailSize.width) thumbnailSize.height = round(CGFloat(thumbnailRequest.thumbnailDimensionPoints) * originalSize.height / originalSize.width)
} else { } else {
thumbnailSize.width = round(CGFloat(thumbnailRequest.thumbnailDimensionPoints) * thumbnailSize.width / thumbnailSize.height) thumbnailSize.width = round(CGFloat(thumbnailRequest.thumbnailDimensionPoints) * originalSize.width / originalSize.height)
thumbnailSize.height = CGFloat(thumbnailRequest.thumbnailDimensionPoints) thumbnailSize.height = CGFloat(thumbnailRequest.thumbnailDimensionPoints)
} }
guard thumbnailSize.width > 0 && thumbnailSize.height > 0 else { guard thumbnailSize.width > 0 && thumbnailSize.height > 0 else {
owsFail("Thumbnail has invalid size.") owsFail("Thumbnail has invalid size.")
return nil return nil
} }
guard originalSize.width < thumbnailSize.width && guard originalSize.width > thumbnailSize.width &&
originalSize.height < thumbnailSize.height else { originalSize.height > thumbnailSize.height else {
owsFail("Thumbnail isn't smaller than the original.") owsFail("Thumbnail isn't smaller than the original.")
return nil return nil
} }

@ -67,9 +67,9 @@ typedef void (^OWSThumbnailCompletion)(UIImage *image);
- (nullable NSURL *)originalMediaURL; - (nullable NSURL *)originalMediaURL;
// TODO: Rename to legacy... // TODO: Rename to legacy...
- (nullable UIImage *)thumbnailImage; - (nullable UIImage *)legacyThumbnailImage;
- (nullable NSData *)thumbnailData; //- (nullable NSData *)legacyThumbnailData;
- (nullable NSString *)thumbnailPath; - (nullable NSString *)legacyThumbnailPath;
+ (BOOL)hasThumbnailForMimeType:(NSString *)contentType; + (BOOL)hasThumbnailForMimeType:(NSString *)contentType;

@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
const CGFloat kMaxVideoStillSize = 1 * 1024; const CGFloat kMaxVideoStillSize = 1 * 1024;
const NSUInteger kThumbnailDimensionPointsSmall = 200; const NSUInteger kThumbnailDimensionPointsSmall = 300;
const NSUInteger kThumbnailDimensionPointsMedium = 800; const NSUInteger kThumbnailDimensionPointsMedium = 800;
// This size is large enough to render full screen. // This size is large enough to render full screen.
const NSUInteger ThumbnailDimensionPointsLarge() { const NSUInteger ThumbnailDimensionPointsLarge() {
@ -129,7 +129,7 @@ const NSUInteger ThumbnailDimensionPointsLarge() {
} }
// This is going to be slow the first time it runs. // This is going to be slow the first time it runs.
[self ensureThumbnail]; [self ensureLegacyThumbnail];
return self; return self;
} }
@ -137,7 +137,7 @@ const NSUInteger ThumbnailDimensionPointsLarge() {
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction - (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
[super saveWithTransaction:transaction]; [super saveWithTransaction:transaction];
[self ensureThumbnail]; [self ensureLegacyThumbnail];
} }
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion - (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion
@ -271,7 +271,7 @@ const NSUInteger ThumbnailDimensionPointsLarge() {
return [[[self class] attachmentsFolder] stringByAppendingPathComponent:self.localRelativeFilePath]; return [[[self class] attachmentsFolder] stringByAppendingPathComponent:self.localRelativeFilePath];
} }
- (nullable NSString *)thumbnailPath - (nullable NSString *)legacyThumbnailPath
{ {
NSString *filePath = self.originalFilePath; NSString *filePath = self.originalFilePath;
if (!filePath) { if (!filePath) {
@ -326,9 +326,9 @@ const NSUInteger ThumbnailDimensionPointsLarge() {
} }
} }
NSString *_Nullable thumbnailPath = self.thumbnailPath; NSString *_Nullable legacyThumbnailPath = self.legacyThumbnailPath;
if (thumbnailPath) { if (legacyThumbnailPath) {
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:thumbnailPath error:&error]; BOOL success = [[NSFileManager defaultManager] removeItemAtPath:legacyThumbnailPath error:&error];
if (error || !success) { if (error || !success) {
DDLogError(@"%@ remove legacy thumbnail failed with: %@", self.logTag, error); DDLogError(@"%@ remove legacy thumbnail failed with: %@", self.logTag, error);
@ -429,28 +429,9 @@ const NSUInteger ThumbnailDimensionPointsLarge() {
[MIMETypeUtil isAnimated:contentType]); [MIMETypeUtil isAnimated:contentType]);
} }
- (nullable UIImage *)thumbnailImage - (nullable NSData *)legacyThumbnailData
{ {
NSString *thumbnailPath = self.thumbnailPath; NSString *thumbnailPath = self.legacyThumbnailPath;
if (!thumbnailPath) {
OWSAssert(!self.isImage && !self.isVideo && !self.isAnimated);
return nil;
}
if (![[NSFileManager defaultManager] fileExistsAtPath:thumbnailPath]) {
// This isn't true for some useful edge cases tested by the Debug UI.
DDLogError(@"%@ missing thumbnail for attachmentId: %@", self.logTag, self.uniqueId);
return nil;
}
return [UIImage imageWithContentsOfFile:self.thumbnailPath];
}
- (nullable NSData *)thumbnailData
{
NSString *thumbnailPath = self.thumbnailPath;
if (!thumbnailPath) { if (!thumbnailPath) {
OWSAssert(!self.isImage && !self.isVideo && !self.isAnimated); OWSAssert(!self.isImage && !self.isVideo && !self.isAnimated);
@ -463,12 +444,12 @@ const NSUInteger ThumbnailDimensionPointsLarge() {
return nil; return nil;
} }
return [NSData dataWithContentsOfFile:self.thumbnailPath]; return [NSData dataWithContentsOfFile:thumbnailPath];
} }
- (void)ensureThumbnail - (void)ensureLegacyThumbnail
{ {
NSString *thumbnailPath = self.thumbnailPath; NSString *thumbnailPath = self.legacyThumbnailPath;
if (!thumbnailPath) { if (!thumbnailPath) {
return; return;
} }
@ -877,7 +858,7 @@ const NSUInteger ThumbnailDimensionPointsLarge() {
- (nullable TSAttachmentStream *)cloneAsThumbnail - (nullable TSAttachmentStream *)cloneAsThumbnail
{ {
NSData *thumbnailData = self.thumbnailData; NSData *thumbnailData = self.legacyThumbnailData;
// Only some media types have thumbnails // Only some media types have thumbnails
if (!thumbnailData) { if (!thumbnailData) {
return nil; return nil;

Loading…
Cancel
Save