| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- /**
- * Copyright (c) 2015-present, Facebook, Inc.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- */
- #import "FBImageUtils.h"
- #import "FBMacros.h"
- #import "FBConfiguration.h"
- static uint8_t PNG_MAGIC[] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
- static const NSUInteger PNG_MAGIC_LEN = 8;
- BOOL FBIsPngImage(NSData *imageData)
- {
- if (nil == imageData || [imageData length] < PNG_MAGIC_LEN) {
- return NO;
- }
- static NSData* pngMagicStartData = nil;
- static dispatch_once_t oncePngToken;
- dispatch_once(&oncePngToken, ^{
- pngMagicStartData = [NSData dataWithBytesNoCopy:(void*)PNG_MAGIC length:PNG_MAGIC_LEN freeWhenDone:NO];
- });
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wassign-enum"
- NSRange range = [imageData rangeOfData:pngMagicStartData options:kNilOptions range:NSMakeRange(0, PNG_MAGIC_LEN)];
- #pragma clang diagnostic pop
- return range.location != NSNotFound;
- }
- NSData *FBToPngData(NSData *imageData) {
- if (nil == imageData || [imageData length] < PNG_MAGIC_LEN) {
- return nil;
- }
- if (FBIsPngImage(imageData)) {
- return imageData;
- }
- UIImage *image = [UIImage imageWithData:imageData];
- return nil == image ? nil : (NSData *)UIImagePNGRepresentation(image);
- }
|