Created
February 2, 2014 09:09
-
-
Save jontelang/8765053 to your computer and use it in GitHub Desktop.
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
// Grab the release point.. duh | |
CGPoint releasePoint = [rec locationInView:self]; | |
// Make sure we don't get negative widths/heights etc. | |
CGRect fromRect = CGRectMake( | |
MIN( startPoint.x, releasePoint.x ), | |
MIN( startPoint.y, releasePoint.y ), | |
MAX( startPoint.x, releasePoint.x ) - MIN( startPoint.x, releasePoint.x ), | |
MAX( startPoint.y, releasePoint.y ) - MIN( startPoint.y, releasePoint.y ) | |
); | |
// Temp | |
NSLog(@"=========DATA========"); | |
NSLog(@"startPoint: %@", NSStringFromCGPoint(startPoint)); | |
NSLog(@"releasePoint: %@", NSStringFromCGPoint(releasePoint)); | |
NSLog(@"fromRect: %@", NSStringFromCGRect(fromRect)); | |
NSLog(@"scale: %f", scale); | |
NSLog(@"i.size: %f", scale);i.size | |
// Scale the area we are going to take, retina is 2x | |
// float scale = [UIScreen mainScreen].scale; | |
fromRect.origin.x *= scale; | |
fromRect.origin.y *= scale; | |
fromRect.size.width *= scale; | |
fromRect.size.height *= scale; | |
// Get the image | |
UIImage *i = _UICreateScreenUIImage(); | |
// If is an iPad, do the rotation thing which for some | |
// reason does not seem to be needed in the phone.. | |
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) | |
{ | |
UIGraphicsBeginImageContext(i.size); | |
[i drawAtPoint:CGPointMake(0, 0)]; | |
i = UIGraphicsGetImageFromCurrentImageContext(); | |
} | |
// Grab the image only form within the CROP-area (fromRect) | |
CGImageRef drawImage = CGImageCreateWithImageInRect(i.CGImage, fromRect); | |
UIImage *newImage = [UIImage imageWithCGImage:drawImage]; | |
CGImageRelease(drawImage); | |
// Just makes sure the rect is within "device" sizes (i.e. not 2x) | |
fromRect.origin.x /= scale; | |
fromRect.origin.y /= scale; | |
fromRect.size.width /= scale; | |
fromRect.size.height /= scale; | |
// Create the imageView | |
imageView = [[UIImageView alloc] initWithFrame:fromRect]; | |
// Some optional things | |
if(shadowEnabled) | |
{ | |
imageView.layer.shadowOffset = CGSizeMake(0, 0); | |
imageView.layer.shadowRadius = 5; | |
imageView.layer.shadowOpacity = 0.5; | |
} | |
if(borderEnabled)imageView.layer.borderWidth = 1; | |
// Set stuff | |
[imageView setFrame:fromRect]; | |
[imageView setImage:newImage]; | |
// Add the pinch gesture | |
imageView.userInteractionEnabled = YES; | |
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)]; | |
[imageView addGestureRecognizer:pinch]; | |
// And add. | |
[self addSubview:imageView]; | |
// Now turn of "edit mode" | |
self.backgroundColor = [UIColor clearColor]; | |
editMode = NO; | |
self.layer.mask = Nil; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment