4

I am currently developing an app to create postcards. However, I am wondering on how to store the picture plus the frame (I have three frames depending on resolution) and some text in UITextView (transparent background).

I need to keep original photo resolution even though frame or screen does not allow more than 320 by 480 pixels (2:3) and insert frame and then text. I do not care much about text as long as I can preserve image within frame.

All I need is advice on how to approach this issue: I need framework classes and perhaps hints into methods (functions, messages) that I can use either to work the image as a bitmap or as a jpeg or png.

Cannot do:

  • My project manager's idea: send it into a converter webpage (I cannot remember http address) in God knows which format and God knows how to indicate webpage to merge all views and then wait for it to get back to the phone and save it. POstcard creation must be done locally in case Ipad users want to create them and then save them into PC or something (personally, I would like to exhaust all resources from iPhone development framework has to offer).

  • Get the current context which merges all three UIViews classes/subclasses. Great for 320 by 480 but not for larger resolutions.


Yeah, thanks a lot for your answer-- that is my approach. I thought there was a better way like handling the image and the text as bitmap data, raw data or something else to preserve original information better.

But I still have one little issue which bugs me a little: The image (inside the frame) must be selected from the image library or the camera controller, but hey, it brings you one object (the image in size to fit the screen) and a dictionary which amazingly brings the original image too. Now, there are two objects. But the postcard must be generated in 4 different resolutions and bringing the four "versions" of the image would create a huge memory overhead.

How can I avoid this? Should I use the reference to the following dictionary elements to bring them from memory, create postcard, send it over network and then destroy them?

NSString *const UIImagePickerControllerMediaURL;

NSString *const UIImagePickerControllerReferenceURL; 

in the obj-c doc are objects that point to original info... I just want to reduce the overhead by alloc and dealloc when needed.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
IAM_SNAKE
  • 61
  • 4

1 Answers1

1

You need to draw your postcard into a new in-memory image. The easiest way to create a new image is the UIGraphicsBeginImageContext() function. You can then use UIKit drawing methods to render background image, text etc. into the new image and get the resulting image via UIGraphicsGetImageFromCurrentImageContext(). Depending on your needs, you could also create a JPEG/PNG from the resulting image and write it to disk, using UIImageJPEGRepresentation().

Here's an example:

UIImage *backgroundImage = [UIImage imageNamed:@"MyPostcardBackground.jpg"];
CGRect frameRect = CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height);
UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:frameRect];
NSString *text = @"Caption here...";
[[UIColor whiteColor] set];
[text drawInRect:frameRect withFont:[UIFont boldSystemFontOfSize:35.0] lineBreakMode:UILineBreakModeWordWrap];
//...
UIImage *postcardImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
omz
  • 53,243
  • 5
  • 129
  • 141
  • Yeah, thanks a lot for your answer---- that is my approach (I thought there was a better way like handling the image a nd the text as bitmap data or something else to preserve original information better), but I still have one little issue, which bugs me a little. – IAM_SNAKE Jan 18 '11 at 14:30
  • 1
    You have been very helpful.... truly. I am really glad there is still kind people to help around when in need. At the http://www.iphonedevsdk.com/ forum wanted to strip me down like 50 USD for that answer, when seeing more complex issues being solved for nothing. – IAM_SNAKE Jan 18 '11 at 14:49