4/15/13

C

iOS Assets Library

#import <AssetsLibrary/AssetsLibrary.h>

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    NSURL * assetUrl = [info objectForKey:UIImagePickerControllerReferenceURL];
    NSLog(@"%@", assetUrl.absoluteString);  //"assets-library://asset/asset.JPG?id=ABC7EF08-B1BF-4FFB-9FF9-4FF70FD0610F&ext=JPG"
    [self loadImageFromAsset:assetUrl];
}

-(void)loadImageFromAsset:(NSURL *)assetUrl
{
    ALAssetsLibrary * assetLib = [[ALAssetsLibrary alloc] init];
    [assetLib assetForURL:assetUrl
              resultBlock:^(ALAsset * asset)
                          {
                              ALAssetRepresentation * assetRep = [asset defaultRepresentation];
                              CGImageRef image = [assetRep fullScreenImage];  //full-screen-image to show edited version
                              [self setImageViewWithImage:image];
                          }
             failureBlock:^(NSError * error)
                          {
                              //log...
                          }];
}

-(void)setImageViewWithImage:(CGImageRef)image
{
    UIImageView * imageView = (UIImageView *)[[self view] viewWithTag:ivTag];
    [imageView setImage:[UIImage imageWithCGImage:image]];
}


-----------------------------------------------------------

Objective-C Delegate Protocol

Use UIResponder touchesBegan:withEvent and a protocol to enable a UIImageView to respond to user touch. Sends message to delegate (view controller in this example), delegate can then respond to image touch by launching image picker, etc


Storyboard - ESTouchImageView Settings


Components




ESTouchViewDelegate (Protocol)
- declares the protocol delegate method esTouchViewMsg
- when the user touches a view, an esTouchViewMsg should be sent to the delegate (class) that conforms to the protocol
//file ESTouchViewDelegate.h
@protocol ESTouchViewDelegate<NSObject>

    -(void)esTouchViewMsg;
@end

ESTouchImageView (Custom UIImageView Class)
- has ESTouchViewDelegate protocol property
- on touchesBegan:withEvent: sends esTouchViewMsg to delegate (class) that conforms to the protocol
//file ESTouchImageView.h
#import "ESTouchViewDelegate.h"

@interface ESTouchImageView: UIImageView

    @property (weak, nonatomic) id<ESTouchViewDelegate> esTouchViewDelegate;
@end


//file ESTouchImageView.m
#import "ESTouchImageView.h"

@implementation ESTouchImageView

    //send touch message to delegate from UIResponder touches began event
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"ESTouchImageView - touchesBegan");
        [self sendESTouchViewMsgToDelegate];
    }

    -(void)sendESTouchViewMsgToDelegate
    {
        NSLog(@"ESTouchImageView - sendESTouchViewMsgToDelegate");
        [[self esTouchViewDelegate] esTouchViewMsg];
    }
@end

ESViewController (Delegate)
- has ESTouchImageView control
- conforms to the ESTouchViewDelegate protocol
- is the delegate (ESTouchViewDelegate) for ESTouchImageView
- sets self (view controller) as delegate using IBOutlet property (or viewWithTag reference)
- will respond to the esTouchViewMsg delegate method
//file ESViewController.h
#import "ESTouchImageView.h"

    @interface ESViewController: UIViewController<ESTouchViewDelegate>

        @property (weak, nonatomic) IBOutlet ESTouchImageView * esTouchImageView;
    @end


//file ESViewController.m
#import "ESViewController.h"

@implementation ESViewController

    -(void)viewDidLoad
    {
        [super viewDidLoad];

        //set delegate using IBOutlet property
        [[self esTouchImageView] setEsTouchViewDelegate:self];  //set view controller as delegate

        //set delegate using viewWithTag reference
        //ESTouchImageView * esTouchImageView = (ESTouchImageView *)[[self view] viewWithTag:100];
        //[esTouchImageView setEsTouchViewDelegate:self];  //set view controller as delegate
    }

    -(void)esTouchViewMsg
    {
        NSLog(@"ESViewController - esTouchViewMsg");
        //launch image picker, etc
    }
@end

NSLog Output
ESTouchImageView - touchesBegan
ESTouchImageView - sendESTouchViewMsgToDelegate
ESViewController - esTouchViewMsg

-----------------------------------------------------------

Objective-C Delegate Protocol

On scene exit, send exit message from destination/presented view to the source/presenting view, pass bool indicating cancelled
//file ESExitSceneDelegate.h
@protocol ESExitSceneDelegate<NSObject>

    @optional
    -(void)esExitSceneMsg;
    -(void)esExitSceneMsgCancelled:(BOOL)cancelled;
@end


//file ESEditViewController.h
#import "ESExitSceneDelegate.h"  //protocol

@interface ESEditViewController: UITableViewController

    @property (weak, nonatomic) id<ESExitSceneDelegate> esExitSceneDelegate;
 // @property (weak, nonatomic) IBOutlet UIBarButtonItem * uiCancelBtn;
    @property (weak, nonatomic) IBOutlet UIBarButtonItem * uiSaveBtn;
@end


//file ESEditViewController.m
#import "ESEditViewController.h"

@implementation ESEditViewController
{
    //ivar referencing outlet, alt to prop
    IBOutlet UIBarButtonItem * uiCancelBtn;
}

    -(IBAction)cancel:(id)sender
    {
        [[self navigationController] popToRootViewControllerAnimated:YES];
        [[self esExitSceneDelegate] esExitSceneMsgCancelled:YES];
    }

    -(IBAction)save:(id)sender
    {
        [[self navigationController] popToRootViewControllerAnimated:YES];
        [[self esExitSceneDelegate] esExitSceneMsgCancelled:NO];
    }


//file ESViewController.h
#import "ESExitSceneDelegate.h"  //protocol

@interface ESViewController: UIViewController<ESExitSceneDelegate>
@end


//file ESViewController.m
#import "ESEditViewController.h"
#import "ESViewController.h"

@implementation ESViewController

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        ESEditViewController * esEditViewController = segue.destinationViewController;
        [esEditViewController setEsExitSceneDelegate:self];  //set delegate
    }

    -(void)esExitSceneMsgCancelled:(BOOL)cancelled
    {
        if (cancelled)
        {
            //...
        }
        else
        {
            //...
        }
    }
@end

No comments:

Post a Comment