Skip to content

Instantly share code, notes, and snippets.

@imrekel
Created November 12, 2012 13:43
Show Gist options
  • Save imrekel/4059462 to your computer and use it in GitHub Desktop.
Save imrekel/4059462 to your computer and use it in GitHub Desktop.
bme-ios - PhotoOrganizer
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
{
_selectedPhoto.transform =
CGAffineTransformRotate(_selectedPhoto.transform, -M_PI_2);
}
break;
case 1:
{
_selectedPhoto.transform =
CGAffineTransformRotate(_selectedPhoto.transform, M_PI_2);
}
break;
default:
break;
}
}
- (void)handlePhotoLongPress:(UIGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
_selectedPhoto = (POPhotoView*)sender.view;
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Műveletek"
delegate:self
cancelButtonTitle:@"Mégsem"
destructiveButtonTitle:nil
otherButtonTitles:@"Forgatás balra", @"Forgatás jobbra", @"Szürkeárnyalatos", nil];
[actionSheet showFromRect:sender.view.frame inView:self.view animated:YES];
}
}
- (void)handlePhotoTap:(UITapGestureRecognizer*)sender
{
POPhotoView* photo = (POPhotoView*)sender.view;
[photo removeFromSuperview];
[self.view addSubview:photo];
if (photo.isExpanded)
{
photo.isExpanded = NO;
photo.bounds = photo.originalBounds;
photo.center = photo.originalCenter;
}
else
{
photo.originalBounds = photo.bounds;
photo.originalCenter = photo.center;
photo.isExpanded = YES;
CGPoint center = CGPointMake(
self.view.bounds.size.width / 2, photo.center.y);
if (center.y < photo.bounds.size.height * 1.5)
center.y = photo.bounds.size.height * 1.5 + kPhotoPadding;
photo.center = center;
photo.bounds = CGRectMake(0, 0, photo.bounds.size.width * 3,
photo.bounds.size.height * 3);
}
}
- (void)performOnAllPhotosAnimated:(void (^)(POPhotoView* photo))photoOperation
{
for (UIView* view in self.view.subviews)
{
if ([view isKindOfClass:[POPhotoView class]])
{
[UIView animateWithDuration:0.5 delay:0
options:UIViewAnimationOptionCurveEaseInOut |
UIViewAnimationOptionLayoutSubviews
animations:^{
photoOperation((POPhotoView*)view);
} completion:nil];
}
}
}
+ (UIImage*)transformImageToGrayScale:(UIImage*)image
{
CGContextRef ctx;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
int byteIndex = 0;
for (int ii = 0 ; ii < width * height ; ++ii)
{
int outputColor = (rawData[byteIndex] + rawData[byteIndex+1] +
rawData[byteIndex+2]) / 3;
rawData[byteIndex] = (char) (outputColor);
rawData[byteIndex+1] = (char) (outputColor);
rawData[byteIndex+2] = (char) (outputColor);
byteIndex += 4;
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedLast );
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
free(rawData);
return rawImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment