Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Created January 8, 2016 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryuheechul/bea60ae054bb6badecfe to your computer and use it in GitHub Desktop.
Save ryuheechul/bea60ae054bb6badecfe to your computer and use it in GitHub Desktop.
Rescue object to keep information of photo resources with different types (such as UIImage, NSUR, etc)
//
// PhotoResource.h
// Modeling
//
// Created by Heechul on 1/8/16.
// Copyright © 2016 Heechul Ryu. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol PhotoResourcable <NSObject>
@end
@interface UIImage () <PhotoResourcable>
@end
@interface NSURL () <PhotoResourcable>
@end
typedef enum {
PhotoResourceTypeImage,
PhotoResourceTypeUrl,
} PhotoResourceType;
@interface PhotoResource : NSObject
@property (nonatomic, assign, readonly) PhotoResourceType type;
@property (nonatomic, strong, readonly) UIImage *image;
@property (nonatomic, strong, readonly) NSURL *url;
+ (instancetype)resourceWithType:(PhotoResourceType)type resourcePath:(NSString *)resourcePath;
+ (instancetype)resourceWithResourcable:(id<PhotoResourcable>)resourcable;
@end
//
// PhotoResource.m
// Modeling
//
// Created by Heechul on 1/8/16.
// Copyright © 2016 Heechul Ryu. All rights reserved.
//
#import "PhotoResource.h"
@interface PhotoResource ()
@property (nonatomic, assign) PhotoResourceType type;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSURL *url;
@end
@implementation PhotoResource
+ (instancetype)resourceWithResourcable:(id<PhotoResourcable>)resourcable
{
PhotoResource *resource = [[PhotoResource alloc] initWithResourcable:resourcable];
return resource;
}
+ (instancetype)resourceWithType:(PhotoResourceType)type resourcePath:(NSString *)resourcePath
{
switch (type) {
case PhotoResourceTypeUrl:
return [self resourceWithResourcable:[NSURL URLWithString:resourcePath]];
case PhotoResourceTypeImage:
return [self resourceWithResourcable:[UIImage imageNamed:resourcePath]];
default:
break;
}
}
- (id)initWithResourcable:(id<PhotoResourcable>)resourcable
{
self = [self init];
if (self) {
if ([resourcable isKindOfClass:[NSURL class]]) {
self.type = PhotoResourceTypeUrl;
self.url = (NSURL *) resourcable;
}
else if ([resourcable isKindOfClass:[UIImage class]]) {
self.type = PhotoResourceTypeImage;
self.image = (UIImage *) resourcable;
}
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment