|
| 1 | +// |
| 2 | +// FDTakeController.m |
| 3 | +// FDTakeExample |
| 4 | +// |
| 5 | +// Created by Will Entriken on 8/9/12. |
| 6 | +// Copyright (c) 2012 William Entriken. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "FDTakeController.h" |
| 10 | + |
| 11 | +@interface FDTakeController() <UIActionSheetDelegate, UIAlertViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> |
| 12 | +@property (strong, nonatomic) NSMutableArray *sources; |
| 13 | +@property (strong, nonatomic) NSMutableArray *buttonTitles; |
| 14 | +@property (strong, nonatomic) UIActionSheet *actionSheet; |
| 15 | +@property (strong, nonatomic) UIPopoverController *popover; |
| 16 | + |
| 17 | +// Returns either optional view controll for presenting or main window. |
| 18 | +- (UIViewController*)presentingViewController; |
| 19 | + |
| 20 | +@end |
| 21 | + |
| 22 | +@implementation FDTakeController |
| 23 | +@synthesize sources = _sources; |
| 24 | +@synthesize buttonTitles = _buttonTitles; |
| 25 | +@synthesize actionSheet = _actionSheet; |
| 26 | +@synthesize imagePicker = _imagePicker; |
| 27 | +@synthesize popover = _popover; |
| 28 | +@synthesize viewControllerForPresenting = _viewControllerForPresenting; |
| 29 | +@synthesize popOverPresentRect = _popOverPresentRect; |
| 30 | + |
| 31 | ++ (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize; |
| 32 | +{ |
| 33 | + CGSize imageSize = sourceImage.size; |
| 34 | + CGFloat width = imageSize.width; |
| 35 | + CGFloat height = imageSize.height; |
| 36 | + CGFloat targetWidth = targetSize.width; |
| 37 | + CGFloat targetHeight = targetSize.height; |
| 38 | + CGFloat scaleFactor = 0.0; |
| 39 | + CGFloat scaledWidth = targetWidth; |
| 40 | + CGFloat scaledHeight = targetHeight; |
| 41 | + CGPoint thumbnailPoint = CGPointMake(0.0,0.0); |
| 42 | + |
| 43 | + if (CGSizeEqualToSize(imageSize, targetSize) == NO) { |
| 44 | + CGFloat widthFactor = targetWidth / width; |
| 45 | + CGFloat heightFactor = targetHeight / height; |
| 46 | + |
| 47 | + if (widthFactor > heightFactor) { |
| 48 | + scaleFactor = widthFactor; // scale to fit height |
| 49 | + } |
| 50 | + else { |
| 51 | + scaleFactor = heightFactor; // scale to fit width |
| 52 | + } |
| 53 | + |
| 54 | + scaledWidth = width * scaleFactor; |
| 55 | + scaledHeight = height * scaleFactor; |
| 56 | + |
| 57 | + // center the image |
| 58 | + if (widthFactor > heightFactor) { |
| 59 | + thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; |
| 60 | + } |
| 61 | + else if (widthFactor < heightFactor) { |
| 62 | + thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + CGImageRef imageRef = [sourceImage CGImage]; |
| 67 | + CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); |
| 68 | + CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); |
| 69 | + |
| 70 | + if (bitmapInfo == kCGImageAlphaNone) { |
| 71 | + bitmapInfo = kCGImageAlphaNoneSkipLast; |
| 72 | + } |
| 73 | + |
| 74 | + CGContextRef bitmap; |
| 75 | + |
| 76 | + if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { |
| 77 | + bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); |
| 78 | + |
| 79 | + } else { |
| 80 | + bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + // In the right or left cases, we need to switch scaledWidth and scaledHeight, |
| 85 | + // and also the thumbnail point |
| 86 | + if (sourceImage.imageOrientation == UIImageOrientationLeft) { |
| 87 | + thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); |
| 88 | + CGFloat oldScaledWidth = scaledWidth; |
| 89 | + scaledWidth = scaledHeight; |
| 90 | + scaledHeight = oldScaledWidth; |
| 91 | + |
| 92 | + CGContextRotateCTM (bitmap, M_PI/2); |
| 93 | + CGContextTranslateCTM (bitmap, 0, -targetHeight); |
| 94 | + |
| 95 | + } else if (sourceImage.imageOrientation == UIImageOrientationRight) { |
| 96 | + thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); |
| 97 | + CGFloat oldScaledWidth = scaledWidth; |
| 98 | + scaledWidth = scaledHeight; |
| 99 | + scaledHeight = oldScaledWidth; |
| 100 | + |
| 101 | + CGContextRotateCTM (bitmap, -M_PI/2); |
| 102 | + CGContextTranslateCTM (bitmap, -targetWidth, 0); |
| 103 | + |
| 104 | + } else if (sourceImage.imageOrientation == UIImageOrientationUp) { |
| 105 | + // NOTHING |
| 106 | + } else if (sourceImage.imageOrientation == UIImageOrientationDown) { |
| 107 | + CGContextTranslateCTM (bitmap, targetWidth, targetHeight); |
| 108 | + CGContextRotateCTM (bitmap, M_PI); |
| 109 | + } |
| 110 | + |
| 111 | + CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef); |
| 112 | + CGImageRef ref = CGBitmapContextCreateImage(bitmap); |
| 113 | + UIImage* newImage = [UIImage imageWithCGImage:ref]; |
| 114 | + |
| 115 | + CGContextRelease(bitmap); |
| 116 | + CGImageRelease(ref); |
| 117 | + |
| 118 | + return newImage; |
| 119 | +} |
| 120 | + |
| 121 | +- (UIImagePickerController *)imagePicker |
| 122 | +{ |
| 123 | + if (!_imagePicker) { |
| 124 | + _imagePicker = [[UIImagePickerController alloc] init]; |
| 125 | + _imagePicker.delegate = self; |
| 126 | + } |
| 127 | + return _imagePicker; |
| 128 | +} |
| 129 | + |
| 130 | +- (UIPopoverController *)popover |
| 131 | +{ |
| 132 | + if (!_popover) _popover = [[UIPopoverController alloc] initWithContentViewController:self.imagePicker]; |
| 133 | + return _popover; |
| 134 | +} |
| 135 | + |
| 136 | +- (void)takePhotoOrChooseFromLibrary |
| 137 | +{ |
| 138 | + self.sources = [[NSMutableArray alloc] init]; |
| 139 | + self.buttonTitles = [[NSMutableArray alloc] init]; |
| 140 | + |
| 141 | + if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { |
| 142 | + [self.sources addObject:[NSNumber numberWithInteger:UIImagePickerControllerSourceTypeCamera]]; |
| 143 | + |
| 144 | + [self.buttonTitles addObject:NSLocalizedStringFromTable(@"takePhoto", @"FDTake", @"Option to take photo using camera")]; |
| 145 | + } |
| 146 | + if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { |
| 147 | + [self.sources addObject:[NSNumber numberWithInteger:UIImagePickerControllerSourceTypePhotoLibrary]]; |
| 148 | + [self.buttonTitles addObject:NSLocalizedStringFromTable(@"chooseFromLibrary", @"FDTake", @"Option to select photo from library")]; |
| 149 | + } |
| 150 | + else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { |
| 151 | + [self.sources addObject:[NSNumber numberWithInteger:UIImagePickerControllerSourceTypeSavedPhotosAlbum]]; |
| 152 | + [self.buttonTitles addObject:NSLocalizedStringFromTable(@"chooseFromPhotoRoll", @"FDTake", @"Option to select photo from photo roll")]; |
| 153 | + } |
| 154 | + |
| 155 | + if ([self.sources count]) { |
| 156 | + self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil |
| 157 | + delegate:self |
| 158 | + cancelButtonTitle:nil |
| 159 | + destructiveButtonTitle:nil |
| 160 | + otherButtonTitles:nil]; |
| 161 | + for (NSString *title in self.buttonTitles) |
| 162 | + [self.actionSheet addButtonWithTitle:title]; |
| 163 | + [self.actionSheet addButtonWithTitle:NSLocalizedStringFromTable(@"cancel", @"FDTake", @"Decline to proceed with operation")]; |
| 164 | + self.actionSheet.cancelButtonIndex = self.sources.count; |
| 165 | + |
| 166 | + // If on iPad use the present rect and pop over style. |
| 167 | + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
| 168 | + [self.actionSheet showFromRect:self.popOverPresentRect inView:[self presentingViewController].view animated:YES]; |
| 169 | + } |
| 170 | + else { |
| 171 | + // Otherwise use iPhone style action sheet presentation. |
| 172 | + [self.actionSheet showInView:[self presentingViewController].view]; |
| 173 | + } |
| 174 | + } else { |
| 175 | + |
| 176 | + NSString *str = NSLocalizedStringFromTable(@"noSources", @"FDTake", @"There are no sources available to select a photo"); |
| 177 | + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil |
| 178 | + message:str |
| 179 | + delegate:self |
| 180 | + cancelButtonTitle:nil |
| 181 | + otherButtonTitles:nil]; |
| 182 | + |
| 183 | + |
| 184 | + [alertView show]; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +- (void)takeVideoOrChooseFromLibrary |
| 189 | +{ |
| 190 | +#warning TODO |
| 191 | +} |
| 192 | + |
| 193 | +- (void)takePhotoOrVideoOrChooseFromLibrary |
| 194 | +{ |
| 195 | +#warning TODO |
| 196 | +} |
| 197 | + |
| 198 | +#pragma mark - UIActionSheetDelegate |
| 199 | + |
| 200 | +- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex |
| 201 | +{ |
| 202 | + if (buttonIndex == self.actionSheet.cancelButtonIndex) { |
| 203 | + if ([self.delegate respondsToSelector:@selector(takeController:didCancelAfterAttempting:)]) |
| 204 | + [self.delegate takeController:self didCancelAfterAttempting:NO]; |
| 205 | + } else { |
| 206 | + self.imagePicker.sourceType = [[self.sources objectAtIndex:buttonIndex] integerValue]; |
| 207 | + |
| 208 | + // On iPad use pop-overs. |
| 209 | + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
| 210 | + [self.popover presentPopoverFromRect:self.popOverPresentRect |
| 211 | + inView:[self presentingViewController].view |
| 212 | + permittedArrowDirections:UIPopoverArrowDirectionAny |
| 213 | + animated:YES]; |
| 214 | + } |
| 215 | + else { |
| 216 | + // On iPhone use full screen presentation. |
| 217 | + [[self presentingViewController] presentViewController:self.imagePicker animated:YES completion:nil]; |
| 218 | + } |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +#pragma mark - UIAlertViewDelegate |
| 223 | + |
| 224 | +- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex |
| 225 | +{ |
| 226 | + if ([self.delegate respondsToSelector:@selector(takeController:didCancelAfterAttempting:)]) |
| 227 | + [self.delegate takeController:self didCancelAfterAttempting:NO]; |
| 228 | +} |
| 229 | + |
| 230 | +#pragma mark - UIImagePickerControllerDelegate |
| 231 | + |
| 232 | +- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info |
| 233 | +{ |
| 234 | + // If there is an edited image, take that one! |
| 235 | + if ([info objectForKey:UIImagePickerControllerEditedImage]) { |
| 236 | + [self.delegate takeController:self gotPhoto:[info objectForKey:UIImagePickerControllerEditedImage] withInfo:info]; |
| 237 | + } else { |
| 238 | + // Otherwise take the original one. |
| 239 | + [self.delegate takeController:self gotPhoto:[info objectForKey:UIImagePickerControllerOriginalImage] withInfo:info]; |
| 240 | + } |
| 241 | + [picker dismissModalViewControllerAnimated:YES]; |
| 242 | +} |
| 243 | + |
| 244 | +- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker |
| 245 | +{ |
| 246 | + [picker dismissModalViewControllerAnimated:YES]; |
| 247 | + if ([self.delegate respondsToSelector:@selector(takeController:didCancelAfterAttempting:)]) |
| 248 | + [self.delegate takeController:self didCancelAfterAttempting:YES]; |
| 249 | +} |
| 250 | + |
| 251 | +#pragma mark - Presenting view controller method |
| 252 | + |
| 253 | +- (UIViewController*)presentingViewController |
| 254 | +{ |
| 255 | + // Use optional view controller for presenting the image picker if set |
| 256 | + UIViewController *presentingViewController = nil; |
| 257 | + if (self.viewControllerForPresenting!=nil) { |
| 258 | + presentingViewController = self.viewControllerForPresenting; |
| 259 | + } |
| 260 | + else { |
| 261 | + // Otherwise do this stuff (like in original source code) |
| 262 | + presentingViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController]; |
| 263 | + } |
| 264 | + return presentingViewController; |
| 265 | +} |
| 266 | + |
| 267 | + |
| 268 | +@end |
0 commit comments