1

I want many collages in some particular shape like heart shape, star shape, etc... Can any one tell me how can I do this programmatically in my iPhone app with objective c?

6
  • Yes ,For making a collage ull need images, add these images into the UIView and then do what you want.... Can you please make your question a bit more clearer ? What do u actually want ?? Sep 19, 2012 at 10:04
  • Yes , I have done that .thanks but can you tell me how can I put all images in one shape at random position? I want shape collages like this app. itunes.apple.com/in/app/shape-collage/id436917115?mt=8
    – Pinky
    Sep 19, 2012 at 10:08
  • For doing this you would have to use UIGestures or UITouches and u should have multiple images which you can move according to the touch and place them in a collage... I can explain but itll b long ... shud i explain more ? Sep 19, 2012 at 10:10
  • hey thanks man. I got all images in scroll and one by one I can add it in a view and also can rotate it,move it and zoom it. I just want to know that after selecting images how can I put it in a shape together at random position? as in the app I have given link.
    – Pinky
    Sep 19, 2012 at 10:23
  • Ill provide u the code for that but itll b really long ... hold on .. c my answer ... Sep 19, 2012 at 10:26

2 Answers 2

1

Use this code. Here The images are being picked from the PhotoLibrary and then using gestures they are being moved, zoomed etc. I am using All the gestures to help u do virtually any panning, pinching and rotating of any image you put.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

[picker dismissModalViewControllerAnimated:YES];
imageCollage = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 150, 150)];
[imageCollage setUserInteractionEnabled:YES];
imageCollage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,imageCollage.frame                                                          .size.width, imageCollage.frame.size.height)];
holderView.layer.cornerRadius = 6;
holderView.clipsToBounds = YES;
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageCollage.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];

DeleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[DeleteImage setImage:[UIImage imageNamed:@"DeleteRed.png"]];
[holderView addSubview:DeleteImage];
DeleteImage.userInteractionEnabled = YES;

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[DeleteImage addGestureRecognizer:tapRecognizer];

[BaseView addSubview:holderView];

[Trash setImage:[UIImage imageNamed:@"12456977871712665073hrum_trash.svg.med.png"] forState:UIControlStateNormal];
}

   -(void)scale:(id)sender
   {
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

    lastScale = 1.0;
    return;
}   
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];
  }

  -(void)rotate:(id)sender
  {

[BaseView bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

    lastRotation = 0.0;
    return;
}

CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
   }

   -(void)move:(id)sender
  {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
}

translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

[[sender view] setCenter:translatedPoint];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

    CGFloat finalX = translatedPoint.x + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
    CGFloat finalY = translatedPoint.y + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

    if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {

        if(finalX < 0) {

            finalX = 0;
        }

        else if(finalX > 260) {

            finalX = 260;
        }

        if(finalY < 0) {

            finalY = 0;
        }

        else if(finalY > 416) {

            finalY = 416;
        }
    }

    else {

        if(finalX < 0) {

            finalX = 0;
        }

        else if(finalX > 416) {

            finalX = 260;
        }

        if(finalY < 0) {

            finalY = 0;
        }

        else if(finalY > 260) {

            finalY = 416;
        }
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.35];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [[sender view] setCenter:CGPointMake(finalX, finalY)];
    [UIView commitAnimations];
}
   }



      -(void)tapped:(UIGestureRecognizer *)recogniser
      {
    NSLog(@"%@",recogniser.view);
    [Trash setImage:[UIImage imageNamed:@"trash_bin_recycle_quit_terminate_error_cancel_close_exit.png"] forState:UIControlStateNormal];

    [[[recogniser view] superview] removeFromSuperview];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

Any queries about the cod please feel free to ask :)... Happy Coding ..

13
  • Hey I have already implemented it but after getting many images from Elcimagepicker How can I put it together in a shape like heart shape. I don't want to rotate or move that pic. just want to put them in a purticular shape. To show my all pic in a shape.
    – Pinky
    Sep 19, 2012 at 10:44
  • @Pinky you want it arranged from before or do you want the user to arrange it on runtime ? Sep 19, 2012 at 10:47
  • yes, I want to put all selected images directly to selected shape. don't want user to rotate or move it. like in app I have given in link.
    – Pinky
    Sep 19, 2012 at 10:51
  • How many images are u selecting ? Coz only a fixed number of images u can put them in some shape Sep 19, 2012 at 10:55
  • its not limited. but I want to put it together.if there is no space in shape then it will be put on each other. I search it many times but it may be some CGRect code. so I could not do it.
    – Pinky
    Sep 19, 2012 at 11:00
0

If you have little experience with Core Graphics and want to programmatically generate those kinds of shapes, I would recommend you use a Mac app called PaintCode.

You can use it to draw your shapes, and it generates the corresponding Objective-C drawing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.