Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added photo library support code

  • Loading branch information...
commit c0fd58933c094655ad88379b1319cb89ecd135ed 1 parent 69379e9
@MugunthKumar authored
View
2  MKBlockAdditions.h
@@ -8,4 +8,6 @@
typedef void (^DismissBlock)(int buttonIndex);
typedef void (^CancelBlock)();
+typedef void (^PhotoPickedBlock)(UIImage *chosenImage);
+#define kPhotoActionSheetTag 10000
View
5 UIActionSheet+MKBlockAdditions.h
@@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import "MKBlockAdditions.h"
-@interface UIActionSheet (MKBlockAdditions) <UIActionSheetDelegate> {
+@interface UIActionSheet (MKBlockAdditions) <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
}
@@ -32,7 +32,8 @@
+ (void) photoPickerWithTitle:(NSString*) title
showInView:(UIView*) view
- onDismiss:(DismissBlock) dismissed
+ presentVC:(UIViewController*) presentView
+ onPhotoPicked:(PhotoPickedBlock) photoPicked
onCancel:(CancelBlock) cancelled;
@end
View
120 UIActionSheet+MKBlockAdditions.m
@@ -10,10 +10,11 @@
static DismissBlock _dismissBlock;
static CancelBlock _cancelBlock;
+static PhotoPickedBlock _photoPickedBlock;
+static UIViewController *_presentVC;
@implementation UIActionSheet (MKBlockAdditions)
-
+(void) actionSheetWithTitle:(NSString*) title
message:(NSString*) message
buttons:(NSArray*) buttonTitles
@@ -21,6 +22,13 @@ +(void) actionSheetWithTitle:(NSString*) title
onDismiss:(DismissBlock) dismissed
onCancel:(CancelBlock) cancelled
{
+ [UIActionSheet actionSheetWithTitle:title
+ message:message
+ destructiveButtonTitle:nil
+ buttons:buttonTitles
+ showInView:view
+ onDismiss:dismissed
+ onCancel:cancelled];
}
+ (void) actionSheetWithTitle:(NSString*) title
@@ -39,13 +47,19 @@ + (void) actionSheetWithTitle:(NSString*) title
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:[self class]
- cancelButtonTitle:NSLocalizedString(@"Cancel", @"")
+ cancelButtonTitle:nil
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:nil];
for(NSString* thisButtonTitle in buttonTitles)
[actionSheet addButtonWithTitle:thisButtonTitle];
+ [actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")];
+ actionSheet.cancelButtonIndex = [buttonTitles count];
+
+ if(destructiveButtonTitle)
+ actionSheet.cancelButtonIndex ++;
+
if([view isKindOfClass:[UIView class]])
[actionSheet showInView:view];
@@ -61,13 +75,79 @@ + (void) actionSheetWithTitle:(NSString*) title
+ (void) photoPickerWithTitle:(NSString*) title
showInView:(UIView*) view
- onDismiss:(DismissBlock) dismissed
+ presentVC:(UIViewController*) presentVC
+ onPhotoPicked:(PhotoPickedBlock) photoPicked
onCancel:(CancelBlock) cancelled
{
+ [_cancelBlock release];
+ _cancelBlock = [cancelled copy];
+
+ [_photoPickedBlock release];
+ _photoPickedBlock = [photoPicked copy];
+
+ [_presentVC release];
+ _presentVC = [presentVC retain];
+
+ int cancelButtonIndex = -1;
+
+ UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
+ delegate:[self class]
+ cancelButtonTitle:nil
+ destructiveButtonTitle:nil
+ otherButtonTitles:nil];
+
+ if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
+ {
+ [actionSheet addButtonWithTitle:NSLocalizedString(@"Camera", @"")];
+ cancelButtonIndex ++;
+ }
+ if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
+ {
+ [actionSheet addButtonWithTitle:NSLocalizedString(@"Photo library", @"")];
+ cancelButtonIndex ++;
+ }
+ [actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")];
+ cancelButtonIndex ++;
+
+ actionSheet.tag = kPhotoActionSheetTag;
+ actionSheet.cancelButtonIndex = cancelButtonIndex;
+
+ if([view isKindOfClass:[UIView class]])
+ [actionSheet showInView:view];
+
+ if([view isKindOfClass:[UITabBar class]])
+ [actionSheet showFromTabBar:(UITabBar*) view];
+
+ if([view isKindOfClass:[UIBarButtonItem class]])
+ [actionSheet showFromBarButtonItem:(UIBarButtonItem*) view animated:YES];
+
+ [actionSheet release];
+}
+
+
++ (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
+{
+ UIImage *editedImage = (UIImage*) [info valueForKey:UIImagePickerControllerEditedImage];
+ if(!editedImage)
+ editedImage = (UIImage*) [info valueForKey:UIImagePickerControllerOriginalImage];
+
+ _photoPickedBlock(editedImage);
+ [picker dismissModalViewControllerAnimated:YES];
+ [picker autorelease];
+}
+
+
++ (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
+{
+ // Dismiss the image selection and close the program
+ [_presentVC dismissModalViewControllerAnimated:YES];
+ [picker autorelease];
+ [_presentVC release];
+ _cancelBlock();
}
--(void)actionSheet:(UIActionSheet*) actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex
++(void)actionSheet:(UIActionSheet*) actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex
{
if(buttonIndex == [actionSheet cancelButtonIndex])
{
@@ -75,7 +155,37 @@ -(void)actionSheet:(UIActionSheet*) actionSheet didDismissWithButtonIndex:(NSInt
}
else
{
- _dismissBlock(buttonIndex);
+ if(actionSheet.tag == kPhotoActionSheetTag)
+ {
+ if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
+ {
+ buttonIndex ++;
+ }
+ if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
+ {
+ buttonIndex ++;
+ }
+
+
+ UIImagePickerController *picker = [[UIImagePickerController alloc] init];
+ picker.delegate = [self class];
+ picker.allowsEditing = YES;
+
+ if(buttonIndex == 0)
+ {
+ picker.sourceType = UIImagePickerControllerSourceTypeCamera;
+ }
+ else if(buttonIndex == 1)
+ {
+ picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
+ }
+
+ [_presentVC presentModalViewController:picker animated:YES];
+ }
+ else
+ {
+ _dismissBlock(buttonIndex);
+ }
}
}
@end
View
8 UIAlertViewCategory.xcodeproj/project.pbxproj
@@ -35,11 +35,11 @@
AB5E75631336DD7A0016813D /* UIAlertViewCategoryViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIAlertViewCategoryViewController.h; sourceTree = "<group>"; };
AB5E75641336DD7A0016813D /* UIAlertViewCategoryViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UIAlertViewCategoryViewController.m; sourceTree = "<group>"; };
AB5E75671336DD7A0016813D /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/UIAlertViewCategoryViewController.xib; sourceTree = "<group>"; };
- AB5E756E1336DDA20016813D /* UIAlertView+MKBlockAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; path = "UIAlertView+MKBlockAdditions.h"; sourceTree = "<group>"; };
+ AB5E756E1336DDA20016813D /* UIAlertView+MKBlockAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIAlertView+MKBlockAdditions.h"; sourceTree = "<group>"; };
AB5E756F1336DDA20016813D /* UIAlertView+MKBlockAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIAlertView+MKBlockAdditions.m"; sourceTree = "<group>"; };
- AB8805AC133771930021F74B /* UIActionSheet+MKBlockAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; name = "UIActionSheet+MKBlockAdditions.h"; path = "../UIActionSheet+MKBlockAdditions.h"; sourceTree = "<group>"; };
+ AB8805AC133771930021F74B /* UIActionSheet+MKBlockAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIActionSheet+MKBlockAdditions.h"; path = "../UIActionSheet+MKBlockAdditions.h"; sourceTree = "<group>"; };
AB8805AD133771930021F74B /* UIActionSheet+MKBlockAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIActionSheet+MKBlockAdditions.m"; path = "../UIActionSheet+MKBlockAdditions.m"; sourceTree = "<group>"; };
- AB8805B1133771FE0021F74B /* MKBlockAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MKBlockAdditions.h; sourceTree = "<group>"; };
+ AB8805B1133771FE0021F74B /* MKBlockAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MKBlockAdditions.h; path = ../MKBlockAdditions.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -59,7 +59,6 @@
AB5E753F1336DD790016813D = {
isa = PBXGroup;
children = (
- AB8805B1133771FE0021F74B /* MKBlockAdditions.h */,
AB5E75541336DD7A0016813D /* UIAlertViewCategory */,
AB5E754D1336DD790016813D /* Frameworks */,
AB5E754B1336DD790016813D /* Products */,
@@ -94,6 +93,7 @@
AB5E75641336DD7A0016813D /* UIAlertViewCategoryViewController.m */,
AB5E75661336DD7A0016813D /* UIAlertViewCategoryViewController.xib */,
AB5E75551336DD7A0016813D /* Supporting Files */,
+ AB8805B1133771FE0021F74B /* MKBlockAdditions.h */,
AB5E756E1336DDA20016813D /* UIAlertView+MKBlockAdditions.h */,
AB5E756F1336DDA20016813D /* UIAlertView+MKBlockAdditions.m */,
AB8805AC133771930021F74B /* UIActionSheet+MKBlockAdditions.h */,
View
34 UIAlertViewCategory/UIAlertViewCategoryViewController.m
@@ -8,6 +8,7 @@
#import "UIAlertViewCategoryViewController.h"
#import "UIAlertView+MKBlockAdditions.h"
+#import "UIActionSheet+MKBlockAdditions.h"
@implementation UIAlertViewCategoryViewController
@@ -31,6 +32,8 @@ - (void)didReceiveMemoryWarning
- (void)viewDidLoad
{
[super viewDidLoad];
+
+ /*
[UIAlertView alertViewWithTitle:@"Test"
message:@"Hello World"
cancelButtonTitle:@"Dismiss"
@@ -44,6 +47,37 @@ - (void)viewDidLoad
NSLog(@"Cancelled");
}
];
+ */
+
+ /*
+ [UIActionSheet actionSheetWithTitle:@"Title"
+ message:@"Message"
+ destructiveButtonTitle:@"Delete"
+ buttons:[NSArray arrayWithObjects:@"But 1", @"But 2", nil]
+ showInView:self.view
+ onDismiss:^(int buttonIndex)
+ {
+ NSLog(@"%d", buttonIndex);
+ }
+ onCancel:^
+ {
+ NSLog(@"Cancelled");
+ }];
+
+ */
+
+ [UIActionSheet photoPickerWithTitle:@"Choose an avatar"
+ showInView:self.view
+ presentVC:self
+ onPhotoPicked:^(UIImage* image)
+ {
+ NSData *data = UIImagePNGRepresentation(image);
+ [data writeToFile:@"/Users/mugunth/Desktop/test.png" atomically:YES];
+ }
+ onCancel:^
+ {
+ NSLog(@"Cancelled");
+ }];
}
Please sign in to comment.
Something went wrong with that request. Please try again.