0

I am trying to build a photo frame application on iphone. I made the frame it is transparent in png formate, then by choosing photos and was placed behind the frame layer in the interface builder.

In interface builder they are placed well and fit well. Now my problem is how can i save them into one picture.

Here is the code i have, but the saving part keep crashing.

-(IBAction) saveImage:(id)sender{

    imagefront .backgroundColor = [UIColor clearColor]; //This sets your backgroung to transparent. 
    imagefront.opaque = NO;
    [imageView bringSubviewToFront:imagefront];

    UIImage *overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    UIImageWriteToSavedPhotosAlbum(overlappedImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}

Imagefront is the photoframe while imageView is the photo.

Thank you.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
Clarence
  • 1,951
  • 2
  • 34
  • 49

1 Answers1

1

Your current approach is incorrect. You will need to do this to get the image.

UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();

This is assuming that imageView has imageFront as its subview as suggested by the code you've posted.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • Thanks Deepak, However, the it can only save the imageView picture without the imageFront. – Clarence Jun 12 '11 at 10:27
  • @user794575 if `imageFront` is a subview of `imageView`, this will work. I can send you a sample project if you like. – Deepak Danduprolu Jun 12 '11 at 10:29
  • It will be great, Please send to clarence426426@gmail.com. Thank you very much. – Clarence Jun 12 '11 at 10:30
  • And there is a warning saying that renderInContext method not found. Did i forget to import something in there? – Clarence Jun 12 '11 at 10:40
  • Yeah. You need to include `QuartzCore` framework in the project and import the header file like `#import `. – Deepak Danduprolu Jun 12 '11 at 10:42
  • I've uploaded the sample project [`here`](http://dl.dropbox.com/u/22783696/ImageOverlap.zip). Hope it helps you. – Deepak Danduprolu Jun 12 '11 at 10:51
  • Your sample is very helpful indeed. I finally can make it.Thank you very much Deepak. – Clarence Jun 12 '11 at 12:13
  • Oh by the way, there is a minor problem in your sample as well as my project. The problem is the front image position shifts to lower position compare to the position in the interface builder, do you have the same problem? – Clarence Jun 12 '11 at 13:38
  • Yeah basically because I don't adjust the frame. Via IB, I couldn't add an image view as a subview of an image view. So I programmatically add it as a subview. When I do this, it's frame doesn't change. The final location changes based on its superview. In IB it is the window, and later it's the image view. – Deepak Danduprolu Jun 12 '11 at 17:30
  • But that shouldn't be a cause of concern as your subview's layout will be correct w.r.t the parent image view so it should come out fine. – Deepak Danduprolu Jun 12 '11 at 17:31
  • Hi Deepak, I spent a whole night on changing and modifying both the super view and subview. However, the problem still cannot be solved. Do you mind please tell me how can i fix the position of both image? What is the codes and method for it? Thanks – Clarence Jun 13 '11 at 03:28
  • @Clarence do `CGRect subviewFrame = [self.childImageView.superview convertRect:self.childImageView.frame toView:self.parentImageView]; [self.parentImageView addSubview:self.childImageView]; self.childImageView.frame = subviewFrame;` instead of just `[self.parentImageView addSubview:self.childImageView];` in `application:didFinishLaunchingWithOptions:` – Deepak Danduprolu Jun 13 '11 at 08:55
  • Yes! I finally did it. Thank you so so much! – Clarence Jun 13 '11 at 09:17