-
Notifications
You must be signed in to change notification settings - Fork 512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add categories for alternative image types that can be decoded using registered custom decoders #602
Open
brandonli-google
wants to merge
6
commits into
pinterest:master
Choose a base branch
from
brandonli-google:alternative-image-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add categories for alternative image types that can be decoded using registered custom decoders #602
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2307437
Add categories for alternative image types that can be decoded using …
brandonli-google 8267817
Add categories for alternative image types that can be decoded using …
brandonli-google 7b72742
Remove registry of custom decoders from PINRemoteImage, instead allow…
brandonli-google a5a1414
Merge branch 'alternative-image-types' of https://github.com/brandonl…
brandonli-google cce8bf1
Add missing files
brandonli-google 3338561
Merge branch 'master' into alternative-image-types
brandonli-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// PINImage+AlternativeTypes.h | ||
// PINRemoteImage | ||
// | ||
// Created by Brandon Li on 6/22/21. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "PINRemoteImageMacros.h" | ||
|
||
#if PIN_TARGET_IOS | ||
#import <UIKit/UIKit.h> | ||
#elif PIN_TARGET_MAC | ||
#import <Cocoa/Cocoa.h> | ||
#endif | ||
|
||
// A decoder that knows how to convert image data into a PINImage. | ||
@protocol PINImageCustomDecoder | ||
|
||
- (PINImage *)imageFromData:(NSData *)imageData targetSize:(CGSize)size; | ||
|
||
- (BOOL)canRender:(NSData *)imageData; | ||
|
||
@end | ||
|
||
@interface PINImage (AlternativeTypes) | ||
|
||
// The encoded / compressed form of the image data. The data will only be used | ||
// at least one custom decoder that recognizes this data is registered using | ||
// pin_registerCustomDecoder below. | ||
@property(nonatomic, nullable) NSData *pin_encodedImageData; | ||
|
||
// Returns an image of the target size. | ||
- (nullable PINImage *)pin_decodedImageUsingCustomDecodersWithSize:(CGSize)size; | ||
|
||
// Registers a custom image decoder type. | ||
+ (void)pin_registerCustomDecoder:(id<PINImageCustomDecoder>)customDecoder; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// PINImage+AlternativeTypes.m | ||
// PINRemoteImage | ||
// | ||
// Created by Brandon Li on 6/22/21. | ||
// | ||
|
||
#import "PINImage+AlternativeTypes.h" | ||
|
||
#import <objc/runtime.h> | ||
|
||
@implementation PINImage (AlternativeTypes) | ||
|
||
+ (void)pin_registerCustomDecoder:(id<PINImageCustomDecoder>)customDecoder { | ||
NSMutableArray<id<PINImageCustomDecoder>> *decoders = [PINImage pin_decoders]; | ||
[decoders addObject:customDecoder]; | ||
objc_setAssociatedObject(self, @selector(pin_registerCustomDecoder:), decoders, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
|
||
} | ||
|
||
- (nullable NSData *)pin_encodedImageData { | ||
return (NSData *)objc_getAssociatedObject(self, @selector(pin_encodedImageData)); | ||
} | ||
|
||
- (void)setPin_encodedImageData:(NSData *)data { | ||
objc_setAssociatedObject(self, @selector(pin_encodedImageData), data, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
} | ||
|
||
- (nullable PINImage *)pin_decodedImageUsingCustomDecodersWithSize:(CGSize)size { | ||
NSMutableArray<id<PINImageCustomDecoder>> *decoders = [PINImage pin_decoders]; | ||
for (id<PINImageCustomDecoder> decoder in decoders) { | ||
if ([decoder canRender:self.pin_encodedImageData]) { | ||
return [decoder imageFromData:self.pin_encodedImageData targetSize:size]; | ||
} | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
#pragma mark - Private | ||
|
||
+ (NSMutableArray<id<PINImageCustomDecoder>> *)pin_decoders { | ||
return objc_getAssociatedObject(self, @selector(pin_registerCustomDecoder:)) ?: [NSMutableArray array]; | ||
} | ||
|
||
@end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to be using nonatomic here and below? Especially for the class method this seems like it could result in race conditions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good question! It's because I actually do the locking further up the call chain internally where this is used, but your point totally makes sense for more general use
Do you prefer using the atomic keyword or some other locking mechanism in PINRemoteImage?
The priority point is a good one as well. A couple options:
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For associated objects, atomic seems like the easiest in this case and it's used elsewhere in the framework.
I think I like # 2 for the options you presented with a comment about equal priorities. I think that would give enough flexibility without complicating the API too much?
Thanks again for thinking all this through!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some internal discussion I updated the PR to remove the custom decoder registry from PINRemoteImage for now and instead change it to a property with a single decoder to be used that is assigned from outside of PINRemoteImage from the place that invokes this logic. In other words, the decision for which decoder to use for the encoded data is the responsibility of the caller, which simplifies the logic for now.
As far as the locking, I changed the associated object setting to be RETAIN instead of RETAIN_NONATOMIC
The thinking is that we could defer the final design of the decoder registry until we actually have a full solution for the remote image scenario, since for now in our solution we'd like the selection of the decoder to be context-specific, and it's not yet clear how to achieve this in PINRemoteImage which doesn't have knowledge of such contexts
How does that sound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted to bump this!