Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
codeswimmer / ios_crop_image.m
Last active December 10, 2015 12:58
iOS: How To Crop Image
(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
return newImage;
}
If you have implemented that function above. You can crop images using the code below (crop the image "whoeImage.png" to a rectangle (0,0,40,40)).
@codeswimmer
codeswimmer / ios_log_method_name.m
Created January 2, 2013 20:04
iOS: Display Method Name As Log
NSLog(@"%s", __FUNCTION__);
@codeswimmer
codeswimmer / ios_bundle_info.m
Created January 2, 2013 20:04
iOS: Display Bundle Information
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSLog(@"Bundle Info: %@", infoDictionary);
@codeswimmer
codeswimmer / ios_uicolor_system_colors.m
Created January 2, 2013 20:03
iOS: UIColor System Colors
UIColorSystemColors has these values to choose from.
@interface UIColor (UIColorSystemColors)
+ (UIColor *)lightTextColor; // for a dark background
+ (UIColor *)darkTextColor; // for a light background
+ (UIColor *)groupTableViewBackgroundColor;
+ (UIColor *)viewFlipsideBackgroundColor;
+ (UIColor *)scrollViewTexturedBackgroundColor __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_3_2);
+ (UIColor *)underPageBackgroundColor __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_5_0);
@end
@codeswimmer
codeswimmer / ios_array_sorting.m
Created January 2, 2013 20:03
iOS: Array Sorting
Basic Array Sort In Ascending Order.
[SomeArrayName sortUsingSelector:@selector(compare:)];
OR
SomeArrayName = [SomeArrayName sortedArrayUsingSelector:@selector(compare:)];Sorting In Descending Order
Sorting In Descending Order.
NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
@codeswimmer
codeswimmer / ios_file_via_url.m
Created January 2, 2013 20:01
iOS: How To Access Local File Using NSURL
Use [[NSBundle mainBundle] resourceURL] to gain a URL string of your local resource.
For example you have index.html in you bundle resource directory. To create an NSURL of that local html file to display on your UIWebView. Simply do this.
NSString *resourceURLString = [[NSBundle mainBundle] resourceURL];
resourceURLString will return something like this: file:/../AppName.app/
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@index.html",resourceURLString]];
@codeswimmer
codeswimmer / ios_contacts.m
Created January 2, 2013 20:00
iOS: How To Get The Record Of Your Contacts
Add first all required framework to gain access of your device Address Book.
AddressBook.framework and AddressBookUI.framework
Then import these framworks to your controller.
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
There are two ways to get your contacts. First is use the ABPeoplePickerNavigationController, the other is to use the ABAddressBookRef.
@codeswimmer
codeswimmer / ios_remove_subviews.m
Created January 2, 2013 19:59
iOS: How to detect and remove SubViews
for (UIView *myView in MainView.subviews) {
[myView removeFromSuperview];
}
@codeswimmer
codeswimmer / ios_plist_file_access.m
Created January 2, 2013 19:59
iOS: How to access PList File
Check first if PLIST exist
NSArray *myPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [myPath objectAtIndex:0];
NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist", plistName] ];
[myPlistPath retain];
// If it's not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:myPlistPath] ) {
@codeswimmer
codeswimmer / ios_nsthread.m
Created January 2, 2013 19:57
iOS: How to use NSThread
Create Thread
[NSThread detachNewThreadSelector:@selector(myMethod) toTarget:self withObject:nil];
Create a selector method called by our thread
- (void)myMethod {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// code here
[pool release];