Photos Application in iPhone

In this application we will see how to multiple photos select from the photo gallery and we can display, rotate and swipe this photo in the view. So let see how it will worked.

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “PhotosApp”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and notice the template generated a separate nib, PhotosAppViewController.xib for the ImageClick application.

Step 4: We need to add the framework MobileCoreServices.framework

Step 5: Open the PhotosAppViewController.h file and make the following changes:

#import <uikit/UIKit.h>
#import <mobileCoreServices/MobileCoreServices.h>
@interface PhotosAppViewController : UIViewController <uinavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverControllerDelegate, UIGestureRecognizerDelegate>
CGFloat Scale;
CGFloat XPosition;
CGFloat YPosition;



Step 6: Double click the PhotosAppViewController.xib file and open it to the interface Builder. Drag the Toolbar and Bar Button Item from the library and place it to the View window. Now select the button and bring up Connection Inspector and connect Touch Up Inside to the File’s Owner icon and select Photosadd: method and drag the label from library and place it to the view window and bring up Attribute Inspector change the text “Swipe Photos Here ” . Now Save the .xib file, close it and go back to the Xcode.

Step 7: Open the PhotosAppViewController.m file and make the following changes:

#import "PhotosAppViewController.h"
@implementation PhotosAppViewController
- (void)dealloc
[super dealloc];
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
-(IBAction)Photosadd:(id)sender {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setMediaTypes:[NSArray arrayWithObject:kUTTypeImage]];
[controller setDelegate:self];
UIPopoverController *popover = [[UIPopoverController alloc]
[popover setDelegate:self];
[popover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *)info {
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width,
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
[self.view addSubview:holderView];
-(void)scale:(id)sender {
[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
Scale = 1.0;
CGFloat scale = 1.0 - (Scale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale,
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
Scale = [(UIPinchGestureRecognizer*)sender scale];
-(void)move:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
XPosition = [[sender view] center].x;
YPosition = [[sender view] center].y;
translatedPoint = CGPointMake(XPosition+translatedPoint.x, YPosition+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) { finalX = 0; } else if(finalX > 768) {
finalX = 768;
if(finalY < 0) { finalY = 0; } else if(finalY > 1024) {
finalY = 1024;
else {
if(finalX < 0) { finalX = 0; } else if(finalX > 1024) {
finalX = 768;
if(finalY < 0) { finalY = 0; } else if(finalY > 768) {
finalY = 1024;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
-(void)tapped:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
*)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return YES;



Step 8: Now Compile and run the application on the Simulator.

Leave a Comment:

Photos Application in iPhone | iPhone / iPod / iPad Software developing says January 26, 2012

[…] iPhone Tutorial | iPhone iOS4 iPad SDK Development & Programming Blog This entry was posted in IOS Development and tagged Application, iPhone, Photos by sniuff. Bookmark the permalink. […]

Shriram says February 28, 2012

This App is Very Good for all of us ..

So please tell me

How to upload images iPhone to server.
upload to images in iPhone.



Vic says May 24, 2012

Do you have the source code available?

madhusudhan says January 21, 2013



Add Your Reply