2015-07-14 11:06:17 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-07-14 11:06:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTImageUtils.h"
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <tgmath.h>
|
|
|
|
|
2015-11-10 13:03:07 +00:00
|
|
|
#import <ImageIO/ImageIO.h>
|
2015-11-17 17:53:47 +00:00
|
|
|
#import <MobileCoreServices/UTCoreTypes.h>
|
2015-11-10 13:03:07 +00:00
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2015-07-14 11:06:17 +00:00
|
|
|
|
2015-07-21 12:40:06 +00:00
|
|
|
static CGFloat RCTCeilValue(CGFloat value, CGFloat scale)
|
|
|
|
{
|
|
|
|
return ceil(value * scale) / scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CGFloat RCTFloorValue(CGFloat value, CGFloat scale)
|
|
|
|
{
|
|
|
|
return floor(value * scale) / scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CGSize RCTCeilSize(CGSize size, CGFloat scale)
|
|
|
|
{
|
|
|
|
return (CGSize){
|
|
|
|
RCTCeilValue(size.width, scale),
|
|
|
|
RCTCeilValue(size.height, scale)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-25 04:39:02 +00:00
|
|
|
static CGImagePropertyOrientation CGImagePropertyOrientationFromUIImageOrientation(UIImageOrientation imageOrientation)
|
|
|
|
{
|
|
|
|
// see https://stackoverflow.com/a/6699649/496389
|
|
|
|
switch (imageOrientation) {
|
|
|
|
case UIImageOrientationUp: return kCGImagePropertyOrientationUp;
|
|
|
|
case UIImageOrientationDown: return kCGImagePropertyOrientationDown;
|
|
|
|
case UIImageOrientationLeft: return kCGImagePropertyOrientationLeft;
|
|
|
|
case UIImageOrientationRight: return kCGImagePropertyOrientationRight;
|
|
|
|
case UIImageOrientationUpMirrored: return kCGImagePropertyOrientationUpMirrored;
|
|
|
|
case UIImageOrientationDownMirrored: return kCGImagePropertyOrientationDownMirrored;
|
|
|
|
case UIImageOrientationLeftMirrored: return kCGImagePropertyOrientationLeftMirrored;
|
|
|
|
case UIImageOrientationRightMirrored: return kCGImagePropertyOrientationRightMirrored;
|
|
|
|
default: return kCGImagePropertyOrientationUp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
CGRect RCTTargetRect(CGSize sourceSize, CGSize destSize,
|
2016-01-20 19:03:22 +00:00
|
|
|
CGFloat destScale, RCTResizeMode resizeMode)
|
2015-07-14 11:06:17 +00:00
|
|
|
{
|
|
|
|
if (CGSizeEqualToSize(destSize, CGSizeZero)) {
|
|
|
|
// Assume we require the largest size available
|
|
|
|
return (CGRect){CGPointZero, sourceSize};
|
|
|
|
}
|
|
|
|
|
2015-08-11 13:21:12 +00:00
|
|
|
CGFloat aspect = sourceSize.width / sourceSize.height;
|
|
|
|
// If only one dimension in destSize is non-zero (for example, an Image
|
|
|
|
// with `flex: 1` whose height is indeterminate), calculate the unknown
|
|
|
|
// dimension based on the aspect ratio of sourceSize
|
|
|
|
if (destSize.width == 0) {
|
|
|
|
destSize.width = destSize.height * aspect;
|
|
|
|
}
|
|
|
|
if (destSize.height == 0) {
|
|
|
|
destSize.height = destSize.width / aspect;
|
|
|
|
}
|
2015-08-12 14:07:24 +00:00
|
|
|
|
2016-07-19 10:33:37 +00:00
|
|
|
// Calculate target aspect ratio if needed
|
2015-08-11 13:21:12 +00:00
|
|
|
CGFloat targetAspect = 0.0;
|
2016-07-19 10:33:37 +00:00
|
|
|
if (resizeMode != RCTResizeModeCenter &&
|
|
|
|
resizeMode != RCTResizeModeStretch) {
|
2015-07-14 11:06:17 +00:00
|
|
|
targetAspect = destSize.width / destSize.height;
|
|
|
|
if (aspect == targetAspect) {
|
2016-01-20 19:03:22 +00:00
|
|
|
resizeMode = RCTResizeModeStretch;
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (resizeMode) {
|
2016-01-20 19:03:22 +00:00
|
|
|
case RCTResizeModeStretch:
|
2016-06-22 11:13:22 +00:00
|
|
|
case RCTResizeModeRepeat:
|
2015-07-14 11:06:17 +00:00
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
return (CGRect){CGPointZero, RCTCeilSize(destSize, destScale)};
|
2015-07-14 11:06:17 +00:00
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
case RCTResizeModeContain:
|
2015-07-14 11:06:17 +00:00
|
|
|
|
|
|
|
if (targetAspect <= aspect) { // target is taller than content
|
|
|
|
|
2016-07-19 10:33:37 +00:00
|
|
|
sourceSize.width = destSize.width;
|
2015-07-14 11:06:17 +00:00
|
|
|
sourceSize.height = sourceSize.width / aspect;
|
|
|
|
|
|
|
|
} else { // target is wider than content
|
|
|
|
|
2016-07-19 10:33:37 +00:00
|
|
|
sourceSize.height = destSize.height;
|
2015-07-14 11:06:17 +00:00
|
|
|
sourceSize.width = sourceSize.height * aspect;
|
|
|
|
}
|
2016-01-20 19:03:22 +00:00
|
|
|
return (CGRect){
|
|
|
|
{
|
|
|
|
RCTFloorValue((destSize.width - sourceSize.width) / 2, destScale),
|
|
|
|
RCTFloorValue((destSize.height - sourceSize.height) / 2, destScale),
|
|
|
|
},
|
|
|
|
RCTCeilSize(sourceSize, destScale)
|
|
|
|
};
|
2015-07-14 11:06:17 +00:00
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
case RCTResizeModeCover:
|
2015-07-14 11:06:17 +00:00
|
|
|
|
|
|
|
if (targetAspect <= aspect) { // target is taller than content
|
|
|
|
|
2016-07-19 10:33:37 +00:00
|
|
|
sourceSize.height = destSize.height;
|
2015-07-14 11:06:17 +00:00
|
|
|
sourceSize.width = sourceSize.height * aspect;
|
|
|
|
destSize.width = destSize.height * targetAspect;
|
2015-07-21 12:40:06 +00:00
|
|
|
return (CGRect){
|
|
|
|
{RCTFloorValue((destSize.width - sourceSize.width) / 2, destScale), 0},
|
|
|
|
RCTCeilSize(sourceSize, destScale)
|
|
|
|
};
|
2015-07-14 11:06:17 +00:00
|
|
|
|
|
|
|
} else { // target is wider than content
|
|
|
|
|
2016-07-19 10:33:37 +00:00
|
|
|
sourceSize.width = destSize.width;
|
2015-07-14 11:06:17 +00:00
|
|
|
sourceSize.height = sourceSize.width / aspect;
|
|
|
|
destSize.height = destSize.width / targetAspect;
|
2015-07-21 12:40:06 +00:00
|
|
|
return (CGRect){
|
|
|
|
{0, RCTFloorValue((destSize.height - sourceSize.height) / 2, destScale)},
|
|
|
|
RCTCeilSize(sourceSize, destScale)
|
|
|
|
};
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
2016-07-19 10:33:37 +00:00
|
|
|
|
|
|
|
case RCTResizeModeCenter:
|
|
|
|
|
|
|
|
// Make sure the image is not clipped by the target.
|
|
|
|
if (sourceSize.height > destSize.height) {
|
|
|
|
sourceSize.width = destSize.width;
|
|
|
|
sourceSize.height = sourceSize.width / aspect;
|
|
|
|
}
|
|
|
|
if (sourceSize.width > destSize.width) {
|
|
|
|
sourceSize.height = destSize.height;
|
|
|
|
sourceSize.width = sourceSize.height * aspect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (CGRect){
|
|
|
|
{
|
|
|
|
RCTFloorValue((destSize.width - sourceSize.width) / 2, destScale),
|
|
|
|
RCTFloorValue((destSize.height - sourceSize.height) / 2, destScale),
|
|
|
|
},
|
|
|
|
RCTCeilSize(sourceSize, destScale)
|
|
|
|
};
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
CGAffineTransform RCTTransformFromTargetRect(CGSize sourceSize, CGRect targetRect)
|
|
|
|
{
|
|
|
|
CGAffineTransform transform = CGAffineTransformIdentity;
|
|
|
|
transform = CGAffineTransformTranslate(transform,
|
|
|
|
targetRect.origin.x,
|
|
|
|
targetRect.origin.y);
|
|
|
|
transform = CGAffineTransformScale(transform,
|
|
|
|
targetRect.size.width / sourceSize.width,
|
|
|
|
targetRect.size.height / sourceSize.height);
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
CGSize RCTTargetSize(CGSize sourceSize, CGFloat sourceScale,
|
|
|
|
CGSize destSize, CGFloat destScale,
|
2016-01-20 19:03:22 +00:00
|
|
|
RCTResizeMode resizeMode,
|
2015-08-12 14:07:24 +00:00
|
|
|
BOOL allowUpscaling)
|
|
|
|
{
|
|
|
|
switch (resizeMode) {
|
2016-07-19 10:33:37 +00:00
|
|
|
case RCTResizeModeCenter:
|
|
|
|
|
|
|
|
return RCTTargetRect(sourceSize, destSize, destScale, resizeMode).size;
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
case RCTResizeModeStretch:
|
2015-08-12 14:07:24 +00:00
|
|
|
|
|
|
|
if (!allowUpscaling) {
|
|
|
|
CGFloat scale = sourceScale / destScale;
|
|
|
|
destSize.width = MIN(sourceSize.width * scale, destSize.width);
|
|
|
|
destSize.height = MIN(sourceSize.height * scale, destSize.height);
|
|
|
|
}
|
|
|
|
return RCTCeilSize(destSize, destScale);
|
|
|
|
|
|
|
|
default: {
|
|
|
|
|
|
|
|
// Get target size
|
|
|
|
CGSize size = RCTTargetRect(sourceSize, destSize, destScale, resizeMode).size;
|
|
|
|
if (!allowUpscaling) {
|
|
|
|
// return sourceSize if target size is larger
|
|
|
|
if (sourceSize.width * sourceScale < size.width * destScale) {
|
|
|
|
return sourceSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL RCTUpscalingRequired(CGSize sourceSize, CGFloat sourceScale,
|
|
|
|
CGSize destSize, CGFloat destScale,
|
2016-01-20 19:03:22 +00:00
|
|
|
RCTResizeMode resizeMode)
|
2015-07-14 11:06:17 +00:00
|
|
|
{
|
|
|
|
if (CGSizeEqualToSize(destSize, CGSizeZero)) {
|
|
|
|
// Assume we require the largest size available
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precompensate for scale
|
|
|
|
CGFloat scale = sourceScale / destScale;
|
|
|
|
sourceSize.width *= scale;
|
|
|
|
sourceSize.height *= scale;
|
|
|
|
|
|
|
|
// Calculate aspect ratios if needed (don't bother if resizeMode == stretch)
|
|
|
|
CGFloat aspect = 0.0, targetAspect = 0.0;
|
|
|
|
if (resizeMode != UIViewContentModeScaleToFill) {
|
|
|
|
aspect = sourceSize.width / sourceSize.height;
|
|
|
|
targetAspect = destSize.width / destSize.height;
|
|
|
|
if (aspect == targetAspect) {
|
2016-01-20 19:03:22 +00:00
|
|
|
resizeMode = RCTResizeModeStretch;
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (resizeMode) {
|
2016-01-20 19:03:22 +00:00
|
|
|
case RCTResizeModeStretch:
|
2015-07-14 11:06:17 +00:00
|
|
|
|
|
|
|
return destSize.width > sourceSize.width || destSize.height > sourceSize.height;
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
case RCTResizeModeContain:
|
2015-07-14 11:06:17 +00:00
|
|
|
|
|
|
|
if (targetAspect <= aspect) { // target is taller than content
|
|
|
|
|
|
|
|
return destSize.width > sourceSize.width;
|
|
|
|
|
|
|
|
} else { // target is wider than content
|
|
|
|
|
|
|
|
return destSize.height > sourceSize.height;
|
|
|
|
}
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
case RCTResizeModeCover:
|
2015-07-14 11:06:17 +00:00
|
|
|
|
|
|
|
if (targetAspect <= aspect) { // target is taller than content
|
|
|
|
|
|
|
|
return destSize.height > sourceSize.height;
|
|
|
|
|
|
|
|
} else { // target is wider than content
|
|
|
|
|
|
|
|
return destSize.width > sourceSize.width;
|
|
|
|
}
|
2016-06-22 11:13:22 +00:00
|
|
|
|
|
|
|
case RCTResizeModeRepeat:
|
2016-07-19 10:33:37 +00:00
|
|
|
case RCTResizeModeCenter:
|
2016-06-22 11:13:22 +00:00
|
|
|
|
|
|
|
return NO;
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-10 13:03:07 +00:00
|
|
|
|
2016-01-06 11:54:25 +00:00
|
|
|
UIImage *__nullable RCTDecodeImageWithData(NSData *data,
|
|
|
|
CGSize destSize,
|
|
|
|
CGFloat destScale,
|
2016-01-20 19:03:22 +00:00
|
|
|
RCTResizeMode resizeMode)
|
2015-11-10 13:03:07 +00:00
|
|
|
{
|
|
|
|
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
|
|
|
if (!sourceRef) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
// Get original image size
|
2015-11-10 13:03:07 +00:00
|
|
|
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(sourceRef, 0, NULL);
|
|
|
|
if (!imageProperties) {
|
|
|
|
CFRelease(sourceRef);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
NSNumber *width = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
|
|
|
|
NSNumber *height = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
CGSize sourceSize = {width.doubleValue, height.doubleValue};
|
2015-11-10 13:03:07 +00:00
|
|
|
CFRelease(imageProperties);
|
|
|
|
|
|
|
|
if (CGSizeEqualToSize(destSize, CGSizeZero)) {
|
|
|
|
destSize = sourceSize;
|
2015-12-08 11:29:08 +00:00
|
|
|
if (!destScale) {
|
|
|
|
destScale = 1;
|
|
|
|
}
|
|
|
|
} else if (!destScale) {
|
|
|
|
destScale = RCTScreenScale();
|
2015-11-10 13:03:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
if (resizeMode == UIViewContentModeScaleToFill) {
|
|
|
|
// Decoder cannot change aspect ratio, so RCTResizeModeStretch is equivalent
|
|
|
|
// to RCTResizeModeCover for our purposes
|
|
|
|
resizeMode = RCTResizeModeCover;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate target size
|
|
|
|
CGSize targetSize = RCTTargetSize(sourceSize, 1, destSize, destScale, resizeMode, NO);
|
2015-11-10 13:03:07 +00:00
|
|
|
CGSize targetPixelSize = RCTSizeInPixels(targetSize, destScale);
|
|
|
|
CGFloat maxPixelSize = fmax(fmin(sourceSize.width, targetPixelSize.width),
|
|
|
|
fmin(sourceSize.height, targetPixelSize.height));
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
NSDictionary<NSString *, NSNumber *> *options = @{
|
2015-11-10 13:03:07 +00:00
|
|
|
(id)kCGImageSourceShouldAllowFloat: @YES,
|
|
|
|
(id)kCGImageSourceCreateThumbnailWithTransform: @YES,
|
|
|
|
(id)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
|
|
|
|
(id)kCGImageSourceThumbnailMaxPixelSize: @(maxPixelSize),
|
|
|
|
};
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
// Get thumbnail
|
2015-11-10 13:03:07 +00:00
|
|
|
CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options);
|
|
|
|
CFRelease(sourceRef);
|
|
|
|
if (!imageRef) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
// Return image
|
2015-11-10 13:03:07 +00:00
|
|
|
UIImage *image = [UIImage imageWithCGImage:imageRef
|
2015-12-08 11:29:08 +00:00
|
|
|
scale:destScale
|
2015-11-10 13:03:07 +00:00
|
|
|
orientation:UIImageOrientationUp];
|
|
|
|
CGImageRelease(imageRef);
|
|
|
|
return image;
|
|
|
|
}
|
2015-11-17 17:53:47 +00:00
|
|
|
|
2016-01-06 11:54:25 +00:00
|
|
|
NSDictionary<NSString *, id> *__nullable RCTGetImageMetadata(NSData *data)
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
{
|
|
|
|
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
|
|
|
if (!sourceRef) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(sourceRef, 0, NULL);
|
|
|
|
CFRelease(sourceRef);
|
|
|
|
return (__bridge_transfer id)imageProperties;
|
|
|
|
}
|
|
|
|
|
2017-07-25 04:39:02 +00:00
|
|
|
NSData *__nullable RCTGetImageData(UIImage *image, float quality)
|
2015-11-17 17:53:47 +00:00
|
|
|
{
|
2017-07-25 04:39:02 +00:00
|
|
|
NSMutableDictionary *properties = [[NSMutableDictionary alloc] initWithDictionary:@{
|
|
|
|
(id)kCGImagePropertyOrientation : @(CGImagePropertyOrientationFromUIImageOrientation(image.imageOrientation))
|
|
|
|
}];
|
2015-11-17 17:53:47 +00:00
|
|
|
CGImageDestinationRef destination;
|
|
|
|
CFMutableDataRef imageData = CFDataCreateMutable(NULL, 0);
|
2017-07-25 04:39:02 +00:00
|
|
|
CGImageRef cgImage = image.CGImage;
|
|
|
|
if (RCTImageHasAlpha(cgImage)) {
|
2015-11-17 17:53:47 +00:00
|
|
|
// get png data
|
|
|
|
destination = CGImageDestinationCreateWithData(imageData, kUTTypePNG, 1, NULL);
|
|
|
|
} else {
|
|
|
|
// get jpeg data
|
|
|
|
destination = CGImageDestinationCreateWithData(imageData, kUTTypeJPEG, 1, NULL);
|
2017-07-25 04:39:02 +00:00
|
|
|
[properties setValue:@(quality) forKey:(id)kCGImageDestinationLossyCompressionQuality];
|
2015-11-17 17:53:47 +00:00
|
|
|
}
|
2017-07-25 04:39:02 +00:00
|
|
|
CGImageDestinationAddImage(destination, cgImage, (__bridge CFDictionaryRef)properties);
|
2015-11-17 17:53:47 +00:00
|
|
|
if (!CGImageDestinationFinalize(destination))
|
|
|
|
{
|
|
|
|
CFRelease(imageData);
|
|
|
|
imageData = NULL;
|
|
|
|
}
|
|
|
|
CFRelease(destination);
|
|
|
|
return (__bridge_transfer NSData *)imageData;
|
|
|
|
}
|
2016-01-20 19:03:22 +00:00
|
|
|
|
|
|
|
UIImage *__nullable RCTTransformImage(UIImage *image,
|
|
|
|
CGSize destSize,
|
|
|
|
CGFloat destScale,
|
|
|
|
CGAffineTransform transform)
|
|
|
|
{
|
|
|
|
if (destSize.width <= 0 | destSize.height <= 0 || destScale <= 0) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:41:18 +00:00
|
|
|
BOOL opaque = !RCTImageHasAlpha(image.CGImage);
|
|
|
|
UIGraphicsBeginImageContextWithOptions(destSize, opaque, destScale);
|
2016-01-20 19:03:22 +00:00
|
|
|
CGContextRef currentContext = UIGraphicsGetCurrentContext();
|
|
|
|
CGContextConcatCTM(currentContext, transform);
|
|
|
|
[image drawAtPoint:CGPointZero];
|
|
|
|
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
|
2016-01-28 22:24:01 +00:00
|
|
|
UIGraphicsEndImageContext();
|
2016-01-20 19:03:22 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL RCTImageHasAlpha(CGImageRef image)
|
|
|
|
{
|
|
|
|
switch (CGImageGetAlphaInfo(image)) {
|
|
|
|
case kCGImageAlphaNone:
|
|
|
|
case kCGImageAlphaNoneSkipLast:
|
|
|
|
case kCGImageAlphaNoneSkipFirst:
|
|
|
|
return NO;
|
|
|
|
default:
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|