Skip to content

Instantly share code, notes, and snippets.

@SheffieldKevin
Created October 17, 2014 16:38
Show Gist options
  • Select an option

  • Save SheffieldKevin/a06907e163885f249548 to your computer and use it in GitHub Desktop.

Select an option

Save SheffieldKevin/a06907e163885f249548 to your computer and use it in GitHub Desktop.

Revisions

  1. Sheffield Kevin created this gist Oct 17, 2014.
    19 changes: 19 additions & 0 deletions CreateImageProtocol.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    //
    // CreateImageProtocol.h
    // test
    //
    // Created by Kevin Meaney on 17/10/2014.
    // Copyright (c) 2014 Kevin Meaney. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    @protocol CreateImageProtocol <NSObject>

    -(NSInteger)imageWidth;

    @optional
    @property (nonatomic, copy) CGImageRef(^createImage)(NSDictionary *);
    @end

    id<CreateImageProtocol>MakeImageProvider();
    14 changes: 14 additions & 0 deletions ImageProvider.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    //
    // ImageProvider.h
    // test
    //
    // Created by Kevin Meaney on 17/10/2014.
    // Copyright (c) 2014 Kevin Meaney. All rights reserved.
    //

    #import <Foundation/Foundation.h>
    #import "CreateImageProtocol.h"

    @interface ImageProvider : NSObject <CreateImageProtocol>

    @end
    32 changes: 32 additions & 0 deletions ImageProvider.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    //
    // ImageProvider.m
    // test
    //
    // Created by Kevin Meaney on 17/10/2014.
    // Copyright (c) 2014 Kevin Meaney. All rights reserved.
    //

    #import "ImageProvider.h"

    @implementation ImageProvider

    @synthesize createImage;

    -(NSInteger)imageWidth
    {
    if (self.createImage)
    {
    CGImageRef myImage = self.createImage(
    @{ @"imagefilepath" : @"/Users/ktam/Pictures/julieanneInNZ.jpg" });
    if (myImage)
    return CGImageGetWidth(myImage);
    }
    return -1;
    }

    @end

    id<CreateImageProtocol>MakeImageProvider()
    {
    return [[ImageProvider alloc] init];
    }
    17 changes: 17 additions & 0 deletions ImageProvider2.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    //
    // ImageProvider2.h
    // test
    //
    // Created by Kevin Meaney on 17/10/2014.
    // Copyright (c) 2014 Kevin Meaney. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    @interface ImageProvider2 : NSObject

    -(NSInteger)imageWidth;

    @property (nonatomic, copy) CGImageRef(^createImage)(NSDictionary *);

    @end
    27 changes: 27 additions & 0 deletions ImageProvider2.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    //
    // ImageProvider2.m
    // test
    //
    // Created by Kevin Meaney on 17/10/2014.
    // Copyright (c) 2014 Kevin Meaney. All rights reserved.
    //

    #import "ImageProvider2.h"

    @implementation ImageProvider2

    @synthesize createImage;

    -(NSInteger)imageWidth
    {
    if (self.createImage)
    {
    CGImageRef myImage = self.createImage(
    @{ @"imagefilepath" : @"/Users/ktam/Pictures/julieanneInNZ.jpg" });
    if (myImage)
    return CGImageGetWidth(myImage);
    }
    return -1;
    }

    @end
    43 changes: 43 additions & 0 deletions SupplyCreateImage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    //
    // SupplyCreateImage.swift
    // test
    //
    // Created by Kevin Meaney on 17/10/2014.
    // Copyright (c) 2014 Kevin Meaney. All rights reserved.
    //

    import Foundation

    public func makeImage(dict : [ NSObject:AnyObject]! ) -> Unmanaged<CGImageRef>! {
    let thePath = dict["imagefilepath"]! as String
    let jpegURL = NSURL.fileURLWithPath(thePath, isDirectory: false)
    let imageSource = CGImageSourceCreateWithURL(jpegURL, nil)!
    let theImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
    let x = Unmanaged.passUnretained(theImage);
    // x.autorelease()
    return x
    }

    /*
    public func getImageWidth() -> Int {
    var imageProvider:AnyObject = MakeImageProvider()
    imageProvider.setCreateImage?(makeImage)
    imageProvider.imageWidth()
    }
    */

    public func getImageWidth2() -> Int {
    let imageProvider = ImageProvider2()
    imageProvider.createImage = makeImage
    return imageProvider.imageWidth()
    }

    // public class ClassMethodWrapper : NSObject {
    class ClassMethodWrapper : NSObject {
    // public class func getImageWidth() -> Int {
    class func getImageWidth() -> Int {
    let imageProvider = ImageProvider2()
    imageProvider.createImage = makeImage
    return imageProvider.imageWidth()
    }
    }
    21 changes: 21 additions & 0 deletions main.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    //
    // main.m
    // test
    //
    // Created by Kevin Meaney on 17/10/2014.
    // Copyright (c) 2014 Kevin Meaney. All rights reserved.
    //

    #import <Foundation/Foundation.h>
    #import "test-Swift.h"

    int main(int argc, const char * argv[]) {
    @autoreleasepool {
    // NSInteger theWidth = getImageWidth2();
    NSInteger theWidth = ClassMethodWrapper.getImageWidth;
    printf("Image width: %ld\n", theWidth);
    // insert code here...
    // NSLog(@"Hello, World!");
    }
    return 0;
    }