Skip to content
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

Fully fledged flip implementation #597

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ NS_ASSUME_NONNULL_BEGIN
/// Crops a portion of an existing image object and returns it as a new image
/// @param frame The region inside the image (In image pixel space) to crop
/// @param angle If any, the angle the image is rotated at as well
/// @param flip Whether to flip (mirror) the image
/// @param circular Whether the resulting image is returned as a square or a circle
- (nonnull UIImage *)croppedImageWithFrame:(CGRect)frame
angle:(NSInteger)angle
flip:(BOOL)flip
circularClip:(BOOL)circular;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ - (BOOL)hasAlpha
alphaInfo == kCGImageAlphaPremultipliedFirst || alphaInfo == kCGImageAlphaPremultipliedLast);
}

- (UIImage *)croppedImageWithFrame:(CGRect)frame angle:(NSInteger)angle circularClip:(BOOL)circular
- (UIImage *)croppedImageWithFrame:(CGRect)frame angle:(NSInteger)angle flip:(BOOL)flip circularClip:(BOOL)circular
{
UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat new];
format.opaque = !self.hasAlpha && !circular;
Expand All @@ -46,14 +46,18 @@ - (UIImage *)croppedImageWithFrame:(CGRect)frame angle:(NSInteger)angle circular
CGContextAddEllipseInRect(context, (CGRect){CGPointZero, frame.size});
CGContextClip(context);
}

// Flip image when applicable
CGContextTranslateCTM(context, flip ? frame.size.width : 0, 0);
CGContextScaleCTM(context, flip ? -1 : 1, 1);

// Offset the origin (Which is the top left corner) to start where our cropping origin is
CGContextTranslateCTM(context, -frame.origin.x, -frame.origin.y);

// If an angle was supplied, rotate the entire canvas + coordinate space to match
if (angle != 0) {
// Rotation in radians
CGFloat rotation = angle * (M_PI/180.0f);
CGFloat rotation = angle * (M_PI/180.0);

// Work out the new bounding size of the canvas after rotation
CGRect imageBounds = (CGRect){CGPointZero, self.size};
Expand Down
41 changes: 41 additions & 0 deletions Objective-C/TOCropViewController/Categories/UIView+Pixels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// UIView+Pixels.h
//
// Copyright 2024 Jan de Vries. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIView(TOPixels)

///Round point value to nearest physical pixel
- (CGFloat)roundToNearestPixel:(CGFloat)point NS_SWIFT_NAME(roundToNearestPixel(point:));

///Check if two CGFloats (points) round to the same number of physical pixels
- (BOOL)pixelCountOf:(CGFloat)point1 equals:(CGFloat)point2;

///Works like CGRectIntegral() but rounds values to the nearest physical pixel
- (CGRect)CGRectIntegralRetina:(CGRect)rect;

@end

NS_ASSUME_NONNULL_END

52 changes: 52 additions & 0 deletions Objective-C/TOCropViewController/Categories/UIView+Pixels.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// UIView+Pixels.m
//
// Copyright 2024 Jan de Vries. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import "UIView+Pixels.h"

@implementation UIView (TOPixels)

- (CGFloat)roundToNearestPixel:(CGFloat)point {
CGFloat screenScale = 2.0;
if (self.window != nil && self.window.screen != nil) {
screenScale = self.window.screen.scale;
}
return round(point * screenScale) / screenScale;
}

- (BOOL)pixelCountOf:(CGFloat)point1 equals:(CGFloat)point2
{
if (self.window == nil || self.window.screen == nil) {
return point1 == point2;
}
CGFloat screenScale = self.window.screen.scale;
return round(point1*screenScale) == round(point2*screenScale);
}

- (CGRect)CGRectIntegralRetina:(CGRect)rect
{
return CGRectMake([self roundToNearestPixel:rect.origin.x],
[self roundToNearestPixel:rect.origin.y],
[self roundToNearestPixel:rect.size.width],
[self roundToNearestPixel:rect.size.height]);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonnull, nonatomic, readonly) UIImage *image;
@property (nonatomic, readonly) CGRect cropFrame;
@property (nonatomic, readonly) NSInteger angle;
@property (nonatomic, readonly) BOOL flipped;
@property (nonatomic, readonly) BOOL circular;

- (nonnull instancetype)initWithImage:(nonnull UIImage *)image cropFrame:(CGRect)cropFrame angle:(NSInteger)angle circular:(BOOL)circular;
- (nonnull instancetype)initWithImage:(nonnull UIImage *)image cropFrame:(CGRect)cropFrame angle:(NSInteger)angle flipped:(BOOL)flipped circular:(BOOL)circular;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ @interface TOActivityCroppedImageProvider ()
@property (nonatomic, strong, readwrite) UIImage *image;
@property (nonatomic, assign, readwrite) CGRect cropFrame;
@property (nonatomic, assign, readwrite) NSInteger angle;
@property (nonatomic, assign, readwrite) BOOL flipped;
@property (nonatomic, assign, readwrite) BOOL circular;

@property (atomic, strong) UIImage *croppedImage;
Expand All @@ -36,12 +37,13 @@ @interface TOActivityCroppedImageProvider ()

@implementation TOActivityCroppedImageProvider

- (instancetype)initWithImage:(UIImage *)image cropFrame:(CGRect)cropFrame angle:(NSInteger)angle circular:(BOOL)circular
- (instancetype)initWithImage:(UIImage *)image cropFrame:(CGRect)cropFrame angle:(NSInteger)angle flipped:(BOOL)flipped circular:(BOOL)circular
{
if (self = [super initWithPlaceholderItem:[UIImage new]]) {
_image = image;
_cropFrame = cropFrame;
_angle = angle;
_flipped = flipped;
_circular = circular;
}

Expand All @@ -63,12 +65,12 @@ - (id)activityViewController:(UIActivityViewController *)activityViewController
- (id)item
{
//If the user didn't touch the image, just forward along the original
if (self.angle == 0 && CGRectEqualToRect(self.cropFrame, (CGRect){CGPointZero, self.image.size})) {
if (self.angle == 0 && !self.flipped && CGRectEqualToRect(self.cropFrame, (CGRect){CGPointZero, self.image.size})) {
self.croppedImage = self.image;
return self.croppedImage;
}

UIImage *image = [self.image croppedImageWithFrame:self.cropFrame angle:self.angle circularClip:self.circular];
UIImage *image = [self.image croppedImageWithFrame:self.cropFrame angle:self.angle flip:self.flipped circularClip:self.circular];
self.croppedImage = image;
return self.croppedImage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ @implementation TOCropViewControllerTransitioning

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.45f;
return 0.45;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
Expand Down Expand Up @@ -86,21 +86,21 @@ - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionC
}
}

cropViewController.view.alpha = (self.isDismissing ? 1.0f : 0.0f);
cropViewController.view.alpha = (self.isDismissing ? 1.0 : 0.0);
if (imageView) {
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:1.0f initialSpringVelocity:0.7f options:0 animations:^{
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.7 options:0 animations:^{
imageView.frame = self.toFrame;
} completion:^(BOOL complete) {
[UIView animateWithDuration:0.25f animations:^{
imageView.alpha = 0.0f;
[UIView animateWithDuration:0.25 animations:^{
imageView.alpha = 0.0;
}completion:^(BOOL complete) {
[imageView removeFromSuperview];
}];
}];
}

[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
cropViewController.view.alpha = (self.isDismissing ? 0.0f : 1.0f);
cropViewController.view.alpha = (self.isDismissing ? 0.0 : 1.0);
} completion:^(BOOL complete) {
[self reset];
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ NS_ASSUME_NONNULL_BEGIN
@interface TOCroppedImageAttributes : NSObject

@property (nonatomic, readonly) NSInteger angle;
@property (nonatomic, readonly) BOOL flipped;
@property (nonatomic, readonly) CGRect croppedFrame;
@property (nonatomic, readonly) CGSize originalImageSize;

- (instancetype)initWithCroppedFrame:(CGRect)croppedFrame angle:(NSInteger)angle originalImageSize:(CGSize)originalSize;
- (instancetype)initWithCroppedFrame:(CGRect)croppedFrame angle:(NSInteger)angle flipped:(BOOL)flipped originalImageSize:(CGSize)originalSize;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
@interface TOCroppedImageAttributes ()

@property (nonatomic, assign, readwrite) NSInteger angle;
@property (nonatomic, assign, readwrite) BOOL flipped;
@property (nonatomic, assign, readwrite) CGRect croppedFrame;
@property (nonatomic, assign, readwrite) CGSize originalImageSize;

@end

@implementation TOCroppedImageAttributes

- (instancetype)initWithCroppedFrame:(CGRect)croppedFrame angle:(NSInteger)angle originalImageSize:(CGSize)originalSize
- (instancetype)initWithCroppedFrame:(CGRect)croppedFrame angle:(NSInteger)angle flipped:(BOOL)flipped originalImageSize:(CGSize)originalSize
{
if (self = [super init]) {
_angle = angle;
_flipped = flipped;
_croppedFrame = croppedFrame;
_originalImageSize = originalSize;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading