Skip to content
Browse files

added lots of stuff

  • Loading branch information...
1 parent ed79e60 commit 727461bb81210ce5b51f11b732378a344d628b84 @odrobnik odrobnik committed
View
7 Core/DTFoundation.h
@@ -6,9 +6,14 @@
#import "DTZipArchive.h"
// Categories
+#import "NSDictionary+DTError.h"
#import "NSString+DTFormatNumbers.h"
#import "NSString+DTPaths.h"
+#import "NSString+DTURLEncoding.h"
#import "NSURL+DTAppLinks.h"
#import "NSURL+DTPrefLinks.h"
#import "UIImage+DTFoundation.h"
-#import "UIView+DTFoundation.h"
+#import "UIView+DTFoundation.h"
+
+// Utility Functions
+#import "DTUtils.h"
View
14 Core/Source/DTUtils.h
@@ -0,0 +1,14 @@
+//
+// DTUtils.h
+// DTFoundation
+//
+// Created by Oliver Drobnik on 7/18/10.
+// Copyright 2010 Drobnik.com. All rights reserved.
+//
+
+/**
+ Various CoreGraphics-related utility functions
+ */
+
+CGSize sizeThatFitsKeepingAspectRatio(CGSize originalSize, CGSize sizeToFit);
+
View
20 Core/Source/DTUtils.m
@@ -0,0 +1,20 @@
+//
+// CGUtils.m
+// iCatalog
+//
+// Created by Oliver Drobnik on 7/18/10.
+// Copyright 2010 Drobnik.com. All rights reserved.
+//
+
+#import "DTUtils.h"
+
+CGSize sizeThatFitsKeepingAspectRatio(CGSize originalSize, CGSize sizeToFit)
+{
+ CGFloat necessaryZoomWidth = sizeToFit.width / originalSize.width;
+ CGFloat necessaryZoomHeight = sizeToFit.height / originalSize.height;
+
+ CGFloat smallerZoom = MIN(necessaryZoomWidth, necessaryZoomHeight);
+
+ CGSize scaledSize = CGSizeMake(roundf(originalSize.width*smallerZoom), roundf(originalSize.height*smallerZoom));
+ return scaledSize;
+}
View
47 Core/Source/NSDictionary+DTError.h
@@ -0,0 +1,47 @@
+//
+// NSDictionary+DTError.h
+// DTFoundation
+//
+// Created by Oliver Drobnik on 4/16/12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+/**
+ A collection of useful additions for `NSDictionary` to deal Property Lists and also to get error handling for malformed data.
+ */
+
+@interface NSDictionary (DTError)
+
+/**-------------------------------------------------------------------------------------
+ @name Property List Error Handling
+ ---------------------------------------------------------------------------------------
+ */
+
+/**
+ Creates and returns a dictionary using the keys and values found in a file specified by a given path.
+
+ @param URL An URL. The file identified by URL must contain a string representation of a property list whose root object is a dictionary.
+ @param error If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in `NULL`.
+ @return A new dictionary that contains the dictionary at path, or `nil` if there is a file error or if the contents of the file are an invalid representation of a dictionary.
+ */
++ (NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)URL error:(NSError **)error;
+
+/**
+ Creates and returns a dictionary using the keys and values found in a file specified by a given URL.
+
+ @param path A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary.
+ @param error If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in `NULL`.
+ @return A new dictionary that contains the dictionary at path, or `nil` if there is a file error or if the contents of the file are an invalid representation of a dictionary.
+ */
++ (NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path error:(NSError **)error;
+
+/**
+ Creates and returns a dictionary using the keys and values found in the given data.
+
+ @param data The data object identified by data must contain a string representation of a property list whose root object is a dictionary.
+ @param error If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in `NULL`.
+ @return A new dictionary that contains the dictionary at path, or `nil` if there is a file error or if the contents of the file are an invalid representation of a dictionary.
+ */
++ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data error:(NSError **)error;
+
+@end
View
115 Core/Source/NSDictionary+DTError.m
@@ -0,0 +1,115 @@
+//
+// NSDictionary+DTError.m
+// DTFoundation
+//
+// Created by Oliver Drobnik on 4/16/12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+#import "NSDictionary+DTError.h"
+#import <Foundation/Foundation.h>
+
+@implementation NSDictionary (DTError)
+
++ (NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)URL error:(NSError **)error
+{
+ CFPropertyListRef propertyList;
+ CFStringRef errorString;
+ CFDataRef resourceData;
+ Boolean status;
+ SInt32 errorCode;
+
+ // Read the XML file.
+ status = CFURLCreateDataAndPropertiesFromResource(
+ kCFAllocatorDefault,
+ (__bridge CFURLRef)URL,
+ &resourceData, // place to put file data
+ NULL,
+ NULL,
+ &errorCode);
+
+ // Reconstitute the dictionary using the XML data.
+ propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
+ resourceData,
+ kCFPropertyListImmutable,
+ &errorString);
+
+
+ NSDictionary *readDictionary = nil;
+
+ if (resourceData)
+ {
+ readDictionary = [NSDictionary dictionaryWithDictionary:(__bridge NSDictionary *)propertyList];
+ CFRelease(propertyList);
+ CFRelease( resourceData );
+ }
+ else
+ {
+ if (errorString)
+ {
+ if (error)
+ {
+ NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
+ NSString *domain = [infoDict objectForKey:(id)kCFBundleIdentifierKey];
+
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:(__bridge NSString *)errorString
+ forKey:NSLocalizedDescriptionKey];
+ *error = [NSError errorWithDomain:domain code:1 userInfo:userInfo];
+ }
+
+ CFRelease(errorString);
+ }
+ }
+
+ return readDictionary;
+}
+
++ (NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path error:(NSError **)error
+{
+ NSURL *url = [NSURL fileURLWithPath:path];
+ return [NSDictionary dictionaryWithContentsOfURL:url error:error];
+}
+
++ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data error:(NSError **)error
+{
+ CFStringRef errorString;
+
+ // uses toll-free bridging for data into CFDataRef and CFPropertyList into NSDictionary
+ CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (__bridge CFDataRef)data,
+ kCFPropertyListImmutable,
+ &errorString);
+
+ if (!plist)
+ {
+ return nil;
+ }
+
+ // we check if it is the correct type and only return it if it is
+ if ([(__bridge id)plist isKindOfClass:[NSDictionary class]])
+ {
+ return (__bridge NSDictionary *)plist;
+ }
+ else
+ {
+ if (errorString)
+ {
+ if (error)
+ {
+ NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
+ NSString *domain = [infoDict objectForKey:(id)kCFBundleIdentifierKey];
+
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:(__bridge NSString *)errorString
+ forKey:NSLocalizedDescriptionKey];
+ *error = [NSError errorWithDomain:domain code:1 userInfo:userInfo];
+ }
+
+ CFRelease(errorString);
+ }
+
+ // clean up ref
+ CFRelease(plist);
+ return nil;
+ }
+}
+
+@end
View
3 Core/Source/NSString+DTPaths.h
@@ -7,7 +7,8 @@
//
-/** A collection of useful additions for `NSString` to deal with paths.
+/**
+ A collection of useful additions for `NSString` to deal with paths.
*/
@interface NSString (DTPaths)
View
29 Core/Source/NSString+DTURLEncoding.h
@@ -0,0 +1,29 @@
+//
+// NSString+DTURLEncoding.h
+// DTFoundation
+//
+// Created by Oliver Drobnik on 4/16/12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+/**
+ A collection of useful additions for `NSString` to deal with URL encoding.
+ */
+
+@interface NSString (DTURLEncoding)
+
+/**-------------------------------------------------------------------------------------
+ @name Encoding Strings for URLs
+ ---------------------------------------------------------------------------------------
+ */
+
+
+/** Encoding suitable for use in URLs.
+
+ stringByAddingPercentEscapes does not replace serveral characters which are problematics in URLs.
+
+ @return The encoded version of the receiver.
+ */
+- (NSString *)stringByURLEncoding;
+
+@end
View
18 Core/Source/NSString+DTURLEncoding.m
@@ -0,0 +1,18 @@
+//
+// NSString+DTURLEncoding.m
+// DTFoundation
+//
+// Created by Oliver Drobnik on 4/16/12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+#import "NSString+DTURLEncoding.h"
+
+@implementation NSString (DTURLEncoding)
+
+- (NSString *)stringByURLEncoding
+{
+ return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)self, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);
+}
+
+@end
View
27 Core/Source/NSString+DTUtilities.h
@@ -0,0 +1,27 @@
+//
+// NSString+DTUtilities.h
+// DTFoundation
+//
+// Created by Oliver Drobnik on 4/16/12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+
+/**
+ A collection of useful additions for `NSString` to deal with paths.
+ */
+
+@interface NSString (DTUtilities)
+
+/**-------------------------------------------------------------------------------------
+ @name Working with Identifiers
+ ---------------------------------------------------------------------------------------
+ */
+
+/** Creates a new string that contains a generated UUID.
+
+ @return The path to the app's Caches folder.
+ */
++ (NSString *)stringWithUUID;
+
+@end
View
24 Core/Source/NSString+DTUtilities.m
@@ -0,0 +1,24 @@
+//
+// NSString+DTUtilities.m
+// DTFoundation
+//
+// Created by Oliver Drobnik on 4/16/12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+#import "NSString+DTUtilities.h"
+
+@implementation NSString (DTUtilities)
+
++ (NSString *)stringWithUUID
+{
+ CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID
+
+ //get the string representation of the UUID
+ NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
+
+ CFRelease(uuidObj);
+ return uuidString;
+}
+
+@end
View
34 Core/Source/UIImage+DTFoundation.h
@@ -11,6 +11,34 @@
*/
@interface UIImage (DTFoundation)
+
+/**
+ @name Loading from RemoteURLs
+ */
+
+
+/**
+ Creates and returns an image object synchronously by loading the image data from the specified URL and optionally caching it.
+
+ Useful values for cachePolicy are:
+
+ - NSURLRequestUseProtocolCachePolicy (default)
+ - NSURLRequestReloadIgnoringLocalCacheData
+ - NSURLRequestReturnCacheDataElseLoad
+ - NSURLRequestReturnCacheDataDontLoad
+
+ @param URL The URL to load the image from
+ @param cachePolicy The cache policy to apply.
+ @param error An optional output parameter to return an error if the loading fails
+ @returns The image object for the specified URL, or nil if the method could not load the specified image.
+ */
++ (UIImage *)imageWithContentsOfURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy error:(NSError **)error;
+
+
+ /**
+ @name Drawing
+ */
+
/**
Mimicks the way images are drawn differently by UIImageView based on the set content mode.
@param rect The rectangle to drawn in
@@ -33,4 +61,10 @@
- (UIImage *)tileImageInClipRect:(CGRect)clipRect inBounds:(CGRect)bounds scale:(CGFloat)scale;
+/**
+ @name Modifying Images
+ */
+
+- (UIImage *)imageScaledToSize:(CGSize)newSize;
+
@end
View
54 Core/Source/UIImage+DTFoundation.m
@@ -11,8 +11,48 @@
// force this category to be loaded by linker
MAKE_CATEGORIES_LOADABLE(UIImage_DTFoundation);
+
+
@implementation UIImage (DTFoundation)
+#pragma mark Loading
+
++ (UIImage *)imageWithContentsOfURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy error:(NSError **)error
+{
+ NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:cachePolicy timeoutInterval:10.0];
+
+ NSCachedURLResponse *cacheResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
+
+ NSData *data;
+
+ if (cacheResponse)
+ {
+ data = [cacheResponse data];
+ NSLog(@"cache hit");
+ }
+ else {
+ NSLog(@"cache fail");
+ }
+
+
+ NSURLResponse *response;
+ data = [NSURLConnection sendSynchronousRequest:request
+ returningResponse:&response
+ error:error];
+
+ if (!data)
+ {
+ NSLog(@"Error loading image at %@", URL);
+ return nil;
+ }
+
+ UIImage *image = [UIImage imageWithData:data];
+ return image;
+}
+
+
+#pragma mark Drawing
+
- (void)drawInRect:(CGRect)rect withContentMode:(UIViewContentMode)contentMode
{
CGRect drawRect;
@@ -230,4 +270,18 @@ - (UIImage *)tileImageInClipRect:(CGRect)clipRect inBounds:(CGRect)bounds scale:
return retImage;
}
+#pragma mark Modifying Images
+
+- (UIImage *)imageScaledToSize:(CGSize)newSize
+{
+ UIGraphicsBeginImageContextWithOptions(newSize, NO, self.scale);
+ [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
+
+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+
+ UIGraphicsEndImageContext();
+
+ return image;
+}
+
@end

0 comments on commit 727461b

Please sign in to comment.
Something went wrong with that request. Please try again.