#ioscodesnippet
Explore tagged Tumblr posts
ioscodesnippet · 14 years ago
Text
Rendering any UIViews into UIImage in one line
Looks like you'd like to make some snapshots of your application, or maybe capturing partial UI elements on the screen for caching or saving? You can achieve this in just one single line like this.
UIImage *viewSnapshot = [myView toImage];
Add this UIView+JTViewToImage category to your project, and you'll also needed to link <QuartzCore/QuartzCore.h> framework too.
In advance, if you want to make sure you've the exact size of the static image output, try this line instead.
UIImage *viewSnapshot = [myView toImageWithScale:1];
This will tell your app to ignore the screen scale and simply reference to the size of the view bounds.
Updated: 2013-10-14
With iOS 7 faster API, the screen capturing method is about 1.8x faster on my iPhone 5. Actual time elasped was down from 0.09s to 0.05 seconds. Clone the repository to test it in action.
43 notes · View notes
ioscodesnippet · 14 years ago
Text
Creating a placeholder UIImage dynamically with color
Ever needed a placeholder color for your lazy loaded table view cell image view? Typically can create a 1x1 pixel image in PhotoShop, add it to your project, then load it with UIImageNamed. Can't imagine how lots of effort and steps, and if the placeholder color are requested to be updated, you'd have to repeat the process all over again.
If you needed work with this kind of situations a lot, lets do it programmatically and DRY! Consider the following UIImage-JTColor category.
Usage:
#import "UIImage-JTColor.h" UIColor *color = [UIColor lightGrayColor]; // Or your whatever UIColor imageView.image = [UIImage imageWithColor:color];
Nothing magical but will saves you a lot of time.
27 notes · View notes
ioscodesnippet · 14 years ago
Text
Adding fadeout effect to any -[UIViews removeFromSuperview]
Typically you like to do something like below when you wanted to remove a view from its parent view.
[myView removeFromSuperview];
Sometimes it's not that please for a user to see an UI component disappearing suddenly. You'd then consider adding some transition effects, and here's a little code snippet in the UIView+JTRemoveAnimated category for how you can get a fade out effect on view removal. So from now on, you just needed to do this to fade out any UIViews
[myView removeFromSuperviewAnimated]
25 notes · View notes
ioscodesnippet · 14 years ago
Text
Force decompressing UIImage in background to achieve better performance
If you’ve ever experience in loading lots of image in your app from the web, and display in a list form of UIImages in a table view, you’d properly heard of doing lazy loading those images. There are several great loading and caching open source solutions you’d probably already heard of such as SDWebImage, EGOImageLoading, etc.
However, you are still experience slight UI delay when the image finished loading or caching out from the disk. The reason behind is UIKit does extra lazy initialization, and only do expensive decompressing at the time to display or draw. Here’s is code snippet meant to be load from a background thread that force an image to be decompressed into the right format, so that the system don’t have to do extra conversion on display.
So after an image has successfully loaded from the web or cached out, create an operation and decompress the image with 
[UIImage decodedImageWithImage:anImage];
And now you can achieve a smoother scrolling experience for your app. Checkout rs / SDWebImage library which already contains the optimization on github.
18 notes · View notes
ioscodesnippet · 14 years ago
Text
Crop an image in specific rect
You probably aware of the method CGImageCreateWithImageInRect that's already defined in the Foundation framework. It is absolute fine for developers familiar well with CoreGraphics and doesn’t mind manually taking care of the image orientations. We loved UIKit anyway.
Use this UIImage-JTImageCrop category instead.
Now you use this instead of the CoreGraphics method.
+ (UIImage *)imageWithImage:(UIImage *)image cropInRect:(CGRect)rect;
Last but not least, you’d somehow want to further abstract it with a proportional rect value. (Imagine you are defining a cropping area on the screen and want to crop a full sized image, you’d transform the visible area to the full sized photo). A handly method is also created for this purpose.
+ (UIImage *)imageWithImage:(UIImage *)image cropInRelativeRect:(CGRect)rect;
Enjoy the snippet!
13 notes · View notes
ioscodesnippet · 14 years ago
Text
Adding drop shadow on UINavigationBar (before iOS 6)
We've an update on this technique for iOS 6.1, please see
Using custom drop shadows in UINavigationBar (fix for iOS 6.1)
Tumblr media
Somehow adding drop shadows on UINavigationBar using the CALayer property fails for me but I later find it out we just need a little trick there.
// The magic is to have -[UIView clipToBounds] set to NO self.navigationController.navigationBar.clipsToBounds = NO;
Base on the fact this is ioscodesnippet, I know I have to make a really simple UINavigationBar-JTDropShadow category to make our life more easier.
Usage:
- (void)viewDidLoad { [super viewDidLoad]; ... [self.navigationController.navigationBar dropShadowWithOffset:CGSizeMake(0, 3) radius:1 color:[UIColor darkGrayColor] opacity:1]; ... }
Make sure you've already imported QuartzCore.framework in your build settings.
While it is more generic by making it a UIView category, but I’ll leave it simple here to demonstrate the main purpose.
8 notes · View notes
ioscodesnippet · 14 years ago
Text
Splitting an array to several components
Say you'd like to split an array into several arrays each with a specific number of components. // Say your originalArray is [@"A", @"B", @"C", @"D"] NSArray *originalArray = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", nil]; // And we now want the array to be split into two arrays with two per segment // like this [[@"A", @"B"], [@"C", @"D"]] NSArray *newArray = [NSArray splitArray:originalArray componentsPerSegment:1]; And here's the code snippet on how you can do it.
3 notes · View notes
ioscodesnippet · 14 years ago
Text
NSStringf. Simpler printf styled +[NSString stringWithFormat:]
If you thinks that +[NSString stringWithFormat:] is simply annoying.
If you missed the C style string formatter like printf() or NSLog().
[NSString stringWithFormat:@"Why should I type this long?"];
// // JTStringAddition.h // // Created by James Tang on 27/08/2011. // http://ioscodesnippet.tumblr.com/ // NSString *NSStringf(NSString *format, ...); // // JTStringAddition.m // // Created by James Tang on 27/08/2011. // http://ioscodesnippet.tumblr.com/ // #import "JTStringAddition.h" NSString *NSStringf(NSString *format, ...) { va_list ap; NSString *str; va_start(ap,format); str=[[NSString alloc] initWithFormat:format arguments:ap]; va_end(ap); return [str autorelease]; }
NSStringf(@"It's just so much easier. %@", @"Really.");
2 notes · View notes
ioscodesnippet · 12 years ago
Text
Using custom drop shadows in UINavigationBar (fix for iOS 6.1)
This is an update to the Adding Drop Shadow on UINavigationBar which was written prior to iOS 6.0
Since iOS 6.0 Apple introduced the -[UINavigationBar shadowImage] property. We should probably leverage that, which are also UIAppearance selectors.
// Configure your images UIImage *background = [UIImage imageNamed:@"titlebar44"]; UIImage *shadow = [UIImage imageNamed:@"titlebar-bottom-highlight"]; // Using the appearance proxy // Note: setBackgroundImage:forBarMetrics has been documented to be // **compulsory** to enable the displaying of the custom shadow image. [[UINavigationBar appearance] setBackgroundImage:background forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setShadowImage:shadow];
You probably went with similar code, but the custom shadow just didn't show up, right?
The problem occurs because of the implementation on UINavigationBar, the clipsToBounds property is needed to be set to NO, and this step has been missing from the documentation. (At least I didn't find it)
Most probably you'll need to set self.navigationController.navigationBar.clipsToBounds = NO in every UIViewController subclass you wrote, a more convenience way is to subclass UINavigationController and have it do the job for you. And I've created a snippet for you.
So now you can initialise your view controllers in code like this:
UIViewController *controller; // configure your view controller UINavigationController *navController = [[JTDropShadowFixNavigationViewController alloc] initWithRootViewController:controller];
Or if you use storyboard, simply change the subclass of your UINavigationController to JTDropShadowFixNavigationViewController.
Tumblr media
In fact there's an even simpler method you'll notice in the snippet.
This is a hack that adds a category method to always set clipsToBounds to NO for UINavigationBar. Enable it defining this constant somewhere in your code (recommended in .pch):
#define ENABLE_JTDROPSHADOW_GLOBAL_FIX 1
Let me know your thoughts!
0 notes