Ever wanted to have a simple way of previewing an image on iPhone ?
Maybe you have a thumbnail you wish to preview in a larger size ? Or you have a thumbnail downloaded from a url, and if you tap on the thumbnail you want to download the bigger version of the image and preview it ?
Well, you can do all this with this small component. ( download the code at the end of this article )
Let’s see what can you do with this component. In its simplest form, we can use the component to preview a UIImageView’s image:
INImagePreview *preview = [[INImagePreview alloc] init];
[preview previewImage:oneImageView.image parentView:mainView];
[preview release];
In this case we have a UIImageView named oneImageView. We are going to preview the image of this oneImageView. The mainView is going to hold the preview of the image. It looks like this:
The second – and maybe a more useful case is when we tap on a UIImageView but we want to download the bigger version of the thumbnail and display it. This can be achieved with something similar to this:
INImagePreview *preview = [[INImagePreview alloc] init];
[preview previewImageWithUrl:[NSURL URLWithString:@"http://moszi.net/dev/Home_files/server.jpg"]
parentView:mainView];
[preview release];
In this case INImagePreview will show a small UIActivityIndicator until the image is downloaded, then will show the downloaded image.
While this functionality is okay, probably what every developer would do as a next step is to add a UIButton or a UIControl on top of the UIImageView and invoke the code above to preview an image. This is why the next two category methods will be at hand:
[oneImageView previewImageWhenTouchedUpInsideUsingParentView:mainView];
[oneImageView previewUrlWhenTouchedUpInside:[NSURL URLWithString:
@"http://moszi.net/dev/Home_files/server.jpg"] usingParentView:mainView];
The two UIImageView category methods will add functionality to the UIImageView so that when it’s tapped the appropriate image is previewed. The above category methods use Associative Storage (objc_setAssociatedObject) to add an INImagePreview instance to the UIImageView so the functionality can be executed later in time.
You can download the demo project attached to this article. If you like the component drop me a message
- INImagePreview-Demo
