Skip to content

Commit d5eacbd

Browse files
authored
Update TIP to v2.9.2 (#33)
- Style updates - colon ':' align code - split code up more to fit on less horizontal space for easier code reviews - Replace private methods with static C functions - methods have runtime load overhead and binary size overhead - using methods for private code is wasteful to the framework consumer - replace all private methods with static C functions to reduce binary size and improve consuming app launch time perf (slightly) - Twitter Image Pipeline is being a good citizen for consuming apps, every little bit helps - Remove sample app xcscheme dependency of TIP framework for better Carthage support - Credit to: @brentleyjones - Move update to `tip_fetchedImage` in fetch helper so that the implementer of `tip_fetchedImage` can query the current state of the fetch helper at the time the update happens. Can help with animations. - Credit to: @brentleyjones - Optimize image scaling on iOS 10+ to use UIGraphicsImageRenderer - GraphicsRendererSpeed sample app added to validate perf - Added TIPRenderImage to TIPImageUtils - Added tip_imageWithRenderFormattings:render: to UIImage+TIPAdditions
1 parent b285a82 commit d5eacbd

File tree

109 files changed

+5302
-1886
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+5302
-1886
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: objective-c
2-
osx_image: xcode9.2
2+
osx_image: xcode9.3
33
script:
44
./build.sh

CHANGELOG.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22

33
## Info
44

5-
**Document version:** 2.9.0
5+
**Document version:** 2.9.2
66

7-
**Last updated:** 02/01/2018
7+
**Last updated:** 04/27/2018
88

99
**Author:** Nolan O'Brien
1010

1111
## History
1212

13+
### 2.9.2
14+
15+
- Add `TIPRenderImage` util function
16+
- Add `tip_imageWithRenderFormattings:render:` method to `UIImage+TIPAdditions`
17+
18+
### 2.9.1
19+
20+
- Use modern `UIGraphicsImageRenderer` always when scaling images on iOS 10+
21+
- source the `UIGraphicsImageRendererFormat` from the image being scaled, this is optimal
22+
- roughly 10% speed boost
23+
- add _GraphicsRendererSpeed_ project that validates the perf differences
24+
1325
### 2.9.0
1426

1527
- Add P3 color gamut support

Extended/TIPXMP4Codec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// TIPXMP4Codec.h
33
// TwitterImagePipeline
44
//
5-
// Created by Nolan O'Brien on 3/16/17.
5+
// Created on 3/16/17.
66
// Copyright © 2017 Twitter. All rights reserved.
77
//
88

Extended/TIPXMP4Codec.m

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// TIPXMP4Codec.m
33
// TwitterImagePipeline
44
//
5-
// Created by Nolan O'Brien on 3/16/17.
5+
// Created on 3/16/17.
66
// Copyright © 2017 Twitter. All rights reserved.
77
//
88

@@ -11,6 +11,12 @@
1111

1212
@import AVFoundation;
1313

14+
#ifndef PRIVATE_SELF
15+
#define PRIVATE_SELF(type) type * __nullable const self
16+
#endif
17+
18+
NS_ASSUME_NONNULL_BEGIN
19+
1420
#pragma mark - Defer support
1521

1622
typedef void(^tipx_defer_block_t)(void);
@@ -58,7 +64,7 @@ __strong tipx_defer_block_t tipx_macro_concat(tipx_stack_defer_block_, __LINE__)
5864

5965
#pragma mark - Declarations
6066

61-
static CGImageRef TIPX_CGImageCreateFromCMSampleBuffer(CMSampleBufferRef sample) CF_RETURNS_RETAINED;
67+
static CGImageRef __nullable TIPX_CGImageCreateFromCMSampleBuffer(CMSampleBufferRef __nullable sample) CF_RETURNS_RETAINED;
6268

6369
@interface TIPXMP4DecoderConfigInternal : NSObject <TIPXMP4DecoderConfig>
6470
- (instancetype)initWithMaxDecodableFramesCount:(NSUInteger)max;
@@ -90,15 +96,15 @@ - (instancetype)init
9096
return [self initWithDefaultDecoderConfig:nil];
9197
}
9298

93-
- (instancetype)initWithDefaultDecoderConfig:(id<TIPXMP4DecoderConfig>)decoderConfig
99+
- (instancetype)initWithDefaultDecoderConfig:(nullable id<TIPXMP4DecoderConfig>)decoderConfig
94100
{
95101
if (self = [super init]) {
96102
_tip_decoder = [[TIPXMP4Decoder alloc] initWithDefaultDecoderConfig:decoderConfig];
97103
}
98104
return self;
99105
}
100106

101-
- (id<TIPXMP4DecoderConfig>)defaultDecoderConfig
107+
- (nullable id<TIPXMP4DecoderConfig>)defaultDecoderConfig
102108
{
103109
return [(TIPXMP4Decoder *)_tip_decoder defaultDecoderConfig];
104110
}
@@ -141,7 +147,7 @@ - (NSUInteger)tip_frameCount
141147
return _frameCount;
142148
}
143149

144-
- (instancetype)initWithBuffer:(NSMutableData *)buffer config:(id<TIPXMP4DecoderConfig>)config
150+
- (instancetype)initWithBuffer:(NSMutableData *)buffer config:(nullable id<TIPXMP4DecoderConfig>)config
145151
{
146152
if (self = [super init]) {
147153
_data = buffer;
@@ -154,28 +160,34 @@ - (instancetype)initWithBuffer:(NSMutableData *)buffer config:(id<TIPXMP4Decoder
154160

155161
_temporaryFilePath = [[tmpDir stringByAppendingPathComponent:[NSUUID UUID].UUIDString] stringByAppendingPathExtension:@"mp4"];
156162
_temporaryFile = fopen(_temporaryFilePath.UTF8String, "w");
157-
[self _tipx_writeDataToTemporaryFile:_data];
163+
_writeDataToTemporaryFile(self, _data);
158164
}
159165
return self;
160166
}
161167

162168
- (void)dealloc
163169
{
164-
[self _tipx_clear];
170+
_clear(self);
165171
}
166172

167-
- (BOOL)_tipx_writeDataToTemporaryFile:(NSData *)data
173+
static BOOL _writeDataToTemporaryFile(PRIVATE_SELF(TIPXMP4DecoderContext),
174+
NSData *data)
168175
{
169-
if (_temporaryFile) {
176+
if (!self) {
177+
return NO;
178+
}
179+
180+
if (self->_temporaryFile) {
170181
const size_t byteCount = data.length;
171182
if (byteCount) {
172-
const size_t byteOut = fwrite(data.bytes, sizeof(char), byteCount, _temporaryFile);
183+
const size_t byteOut = fwrite(data.bytes, sizeof(char), byteCount, self->_temporaryFile);
173184
if (byteCount == byteOut) {
174185
return YES;
175186
} else {
176-
fclose(_temporaryFile);
177-
_temporaryFile = NULL;
178-
[[NSFileManager defaultManager] removeItemAtPath:_temporaryFilePath error:NULL];
187+
fclose(self->_temporaryFile);
188+
self->_temporaryFile = NULL;
189+
[[NSFileManager defaultManager] removeItemAtPath:self->_temporaryFilePath
190+
error:NULL];
179191
}
180192
}
181193
}
@@ -190,7 +202,7 @@ - (TIPImageDecoderAppendResult)appendData:(NSData *)data
190202
_frameCount = 1; // seed the frames
191203
}
192204
[_data appendData:data];
193-
[self _tipx_writeDataToTemporaryFile:data];
205+
_writeDataToTemporaryFile(self, data);
194206
}
195207

196208
return TIPImageDecoderAppendResultDidProgress;
@@ -203,9 +215,13 @@ - (TIPImageContainer *)_tipx_firstFrameImageContainer
203215
@autoreleasepool {
204216
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:self->_temporaryFilePath]];
205217
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
206-
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil];
218+
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:CMTimeMake(0, 1)
219+
actualTime:nil
220+
error:nil];
207221
if (imageRef) {
208-
UIImage* image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
222+
UIImage* image = [UIImage imageWithCGImage:imageRef
223+
scale:[UIScreen mainScreen].scale
224+
orientation:UIImageOrientationUp];
209225
CFRelease(imageRef);
210226
if (image) {
211227
self->_firstFrame = image;
@@ -218,7 +234,7 @@ - (TIPImageContainer *)_tipx_firstFrameImageContainer
218234
return _firstFrame ? [[TIPImageContainer alloc] initWithImage:_firstFrame] : nil;
219235
}
220236

221-
- (TIPImageContainer *)renderImageWithMode:(TIPImageDecoderRenderMode)mode
237+
- (nullable TIPImageContainer *)renderImageWithMode:(TIPImageDecoderRenderMode)mode
222238
{
223239
if (_cachedContainer) {
224240
return _cachedContainer;
@@ -296,7 +312,8 @@ - (TIPImageContainer *)renderImageWithMode:(TIPImageDecoderRenderMode)mode
296312

297313
TIPImageContainer *container = nil;
298314
if (self->_frameCount > 1) {
299-
UIImage *animatedImage = [UIImage animatedImageWithImages:images duration:CMTimeGetSeconds(self->_avAsset.duration)];
315+
UIImage *animatedImage = [UIImage animatedImageWithImages:images
316+
duration:CMTimeGetSeconds(self->_avAsset.duration)];
300317
container = [[TIPImageContainer alloc] initWithImage:animatedImage];
301318
} else if (self->_frameCount == 1) {
302319
container = [[TIPImageContainer alloc] initWithImage:images.firstObject];
@@ -305,7 +322,7 @@ - (TIPImageContainer *)renderImageWithMode:(TIPImageDecoderRenderMode)mode
305322
self->_cachedContainer = container;
306323
});
307324

308-
[self _tipx_clear];
325+
_clear(self);
309326
return _cachedContainer;
310327
}
311328

@@ -344,18 +361,23 @@ - (TIPImageDecoderAppendResult)finalizeDecoding
344361
}
345362
}
346363

347-
- (void)_tipx_clear
364+
static void _clear(PRIVATE_SELF(TIPXMP4DecoderContext))
348365
{
349-
_avTrack = nil;
350-
_avAsset = nil;
351-
if (_temporaryFile) {
352-
fflush(_temporaryFile);
353-
fclose(_temporaryFile);
354-
_temporaryFile = NULL;
366+
if (!self) {
367+
return;
368+
}
369+
370+
self->_avTrack = nil;
371+
self->_avAsset = nil;
372+
if (self->_temporaryFile) {
373+
fflush(self->_temporaryFile);
374+
fclose(self->_temporaryFile);
375+
self->_temporaryFile = NULL;
355376
}
356-
if (_temporaryFilePath) {
357-
[[NSFileManager defaultManager] removeItemAtPath:_temporaryFilePath error:NULL];
358-
_temporaryFilePath = nil;
377+
if (self->_temporaryFilePath) {
378+
[[NSFileManager defaultManager] removeItemAtPath:self->_temporaryFilePath
379+
error:NULL];
380+
self->_temporaryFilePath = nil;
359381
}
360382
}
361383

@@ -373,7 +395,8 @@ - (instancetype)initWithDefaultDecoderConfig:(nullable id<TIPXMP4DecoderConfig>)
373395
return self;
374396
}
375397

376-
- (TIPImageDecoderDetectionResult)tip_detectDecodableData:(NSData *)data earlyGuessImageType:(NSString *)imageType
398+
- (TIPImageDecoderDetectionResult)tip_detectDecodableData:(NSData *)data
399+
earlyGuessImageType:(nullable NSString *)imageType
377400
{
378401
if (data.length < kSignatureDataRequiredToCheck) {
379402
return TIPImageDecoderDetectionResultNeedMoreData;
@@ -389,7 +412,9 @@ - (TIPImageDecoderDetectionResult)tip_detectDecodableData:(NSData *)data earlyGu
389412
return TIPImageDecoderDetectionResultNoMatch;
390413
}
391414

392-
- (TIPXMP4DecoderContext *)tip_initiateDecoding:(nullable id)config expectedDataLength:(NSUInteger)expectedDataLength buffer:(nullable NSMutableData *)buffer
415+
- (TIPXMP4DecoderContext *)tip_initiateDecoding:(nullable id)config
416+
expectedDataLength:(NSUInteger)expectedDataLength
417+
buffer:(nullable NSMutableData *)buffer
393418
{
394419
id<TIPXMP4DecoderConfig> decoderConfig = nil;
395420
if ([config respondsToSelector:@selector(maxDecodableFramesCount)]) {
@@ -398,15 +423,18 @@ - (TIPXMP4DecoderContext *)tip_initiateDecoding:(nullable id)config expectedData
398423
if (!decoderConfig) {
399424
decoderConfig = self.defaultDecoderConfig;
400425
}
401-
return [[TIPXMP4DecoderContext alloc] initWithBuffer:buffer ?: [[NSMutableData alloc] initWithCapacity:expectedDataLength] config:decoderConfig];
426+
return [[TIPXMP4DecoderContext alloc] initWithBuffer:buffer ?: [[NSMutableData alloc] initWithCapacity:expectedDataLength]
427+
config:decoderConfig];
402428
}
403429

404-
- (TIPImageDecoderAppendResult)tip_append:(TIPXMP4DecoderContext *)context data:(NSData *)data
430+
- (TIPImageDecoderAppendResult)tip_append:(TIPXMP4DecoderContext *)context
431+
data:(NSData *)data
405432
{
406433
return [context appendData:data];
407434
}
408435

409-
- (TIPImageContainer *)tip_renderImage:(TIPXMP4DecoderContext *)context mode:(TIPImageDecoderRenderMode)mode
436+
- (nullable TIPImageContainer *)tip_renderImage:(TIPXMP4DecoderContext *)context
437+
mode:(TIPImageDecoderRenderMode)mode
410438
{
411439
return [context renderImageWithMode:mode];
412440
}
@@ -432,7 +460,7 @@ - (instancetype)initWithMaxDecodableFramesCount:(NSUInteger)max
432460

433461
@end
434462

435-
static CGImageRef TIPX_CGImageCreateFromCMSampleBuffer(CMSampleBufferRef sampleBuffer)
463+
static CGImageRef __nullable TIPX_CGImageCreateFromCMSampleBuffer(CMSampleBufferRef __nullable sampleBuffer)
436464
{
437465
CVImageBufferRef imageBuffer = sampleBuffer ? CMSampleBufferGetImageBuffer(sampleBuffer) : NULL;
438466
if (!imageBuffer) {
@@ -468,3 +496,5 @@ static CGImageRef TIPX_CGImageCreateFromCMSampleBuffer(CMSampleBufferRef sampleB
468496

469497
return CGBitmapContextCreateImage(newContext);
470498
}
499+
500+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)