Draw on a UIImage - iphone

I'm attempting to create a UIImage and save it to the photo gallery, the image gets created but it's just a plane white with no color what so ever. Any ideas?
UIGraphicsBeginImageContext(CGSizeMake(320,420));
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIImage *image = [[UIImage alloc] init];
[image drawInRect:CGRectMake(0, 0, 100, 100)];
UIImage *newImage;
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, self, #selector(image:didFinishSavingWithError:ctx:), nil);
Also just to add to the above. I added this line:
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
And now I get a grey image, still no rectangle, but it's something different so thought it might help

What UIImage are you trying to show?, this code only initialized a space of memory but it's filled with nothing, use [UIImage imageNamed:#"..."]; where the string is some file the resources for your project. Then once you have opened and drawn the image in the context you will be able to draw a string or whatever over it.
Also make sure that you close the context once you have gotten the image from it.

Related

How to position an object over an image and store it

I am creating a "cartoonizer" application, which takes an image as an input and modifies it so as to format it in comics style.
At the moment, the original image is stored in a UIImageView, and I access to it by doing:
imageView.image
I was wondering whether it is possible to position an object on the image imageView.image (like a balloon) and then store the image with the object on it, like if it was originally part of the image content.
Thank you in advance.
You can do it like this, assuming you have correct opacity in the top image.
Like you said you get your image from imageView.image:
From here: blend two uiimages based on alpha/transparency of top image
UIImage *bottomImage = [UIImage imageNamed:#"bottom.png"];
UIImage *image = [UIImage imageNamed:#"top.png"];
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
More compositing techniques here: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-advanced-uiimage-techniques/

Maintain image resolution in screen grab

In my app, the user is able to put stickers on top of a photo. When they go to save their creation, I do a screen grab and store it in a UIImage:
UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
(where self.mainView has a subview UIImageView which holds the photo, and another subview UIView which holds the stickers).
I am wondering, is it possible to do a screen shot in this manner, and maintain the resolution of the aforementioned photo?
The following will 'flatten' two UIImages into one while maintaining the resolution of the original image(s):
CGSize photoSize = photoImage.size;
UIGraphicsBeginImageContextWithOptions(photoSize, NO, 0.0);
CGRect photoRect = CGRectMake(0, 0, photoSize.width, photoSize.height);
// Add the original photo into the context
[photoImage drawInRect:photoRect];
// Add the sticker image with its upper left corner set to where the user placed it
[stickerImage drawAtPoint:stickerView.frame.origin];
// Get the resulting 'flattened' image
UIImage *flattenedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The above assumes photoImage and stickerImage are both instances of UIImage and stickerView is a UIView with containing the stickerImage and thus will be able to use the stickerView frame to determine its origin.
If you have multiple stickers, just iterate through the collection.
If you are looking to save an image of your current view then this might help you.
UIGraphicsBeginImageContext(self.scrollView.contentSize);
[self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect(finalImage.CGImage,
CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y,
scrollView.frame.size.width, scrollView.frame.size.height));
UIImage *screenImage = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
CGImageRelease(imageRef);

Xcode 4.3 How do I save a region of my app screen?

I am creating a native app on Xcode 4.3 and deploying to target iOS 5. The app is a basically a greeting card creator. I am having trouble figuring out how to save a portion of the screen from within the app.
What I want to do is this:
I am offering the user a button, that says "e-mail". when they click the button, the app should 'save' their card as an image and then 'paste' that into an email body.
The reason this is different than other answers on this website is that the area I want to save is made up of 4 'elements'. There is a background graphic that is the tilted card background, then there is a text field where users can type a message and then next to that is a picture area where they can choose their own picture to put on the card.
Here is a photo of what I am talking about:
http://marklopezdesigns.com/mydownloadz!/screenshotCard3.png
How do I save a 'composite' high res of these?
And then how do I get that into an email body message?
The reason i am asking how to 'save' it is because I want to be able to offer users another button that says "save to camera roll" and "send as message". I figure if I can understand how to save this high-res to a variable, then I should be off and running.
Thanks in advance for the help.
========
Here's the solution below
========
...so after a bit of fiddling. Finally got what I wanted. Here's the codebase I have in my method that fires upon touch of the "Save to Album" button:
- (IBAction)savePhoto{
CGRect rect;
rect = CGRectMake(11,50 ,305, 262);
UIView *cardViewer = [[UIView alloc] initWithFrame:rect];
UIGraphicsBeginImageContext(cardViewer.bounds.size);
//make view background transparent
cardViewer.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
cardViewer.opaque = NO;
//stuff items into a subview for capturing
[self.view addSubview:cardViewer];
[cardViewer addSubview:self.tiltedCard];
[cardViewer addSubview:self.bigCardView];
[cardViewer addSubview:self.cardWords];
[cardViewer addSubview:self.photoView];
[cardViewer.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
//put everything back where it belongs
[self.view addSubview:self.tiltedCard];
[self.view addSubview:self.bigCardView];
[self.view addSubview:self.cardWords];
[self.view addSubview:self.photoView];
[cardViewer removeFromSuperview];
}
To capture just an area of the screen, specify the bounds using CGRectMake.
CGRect rect = CGRectMake(50, 50, 100, 100);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
In the example above we are capturing a 100px by 100px region beginning at x:50px and y:50px.
maybe get the picture of the layer by rendering the layer context and grabbing the image, I use this for displaying fancy animations
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
To capture just an area of the screen, use the UIView or SubView and just specify the bounds using CGRectMake.
CGRect rect = CGRectMake(10, 10, 200, 200);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.YourView.layer renderInContext:context]; // Make sure here you are using the greetingcardView rather than self.view
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Note: self.view will capture the whole screen so simply render the view wherever.

Drawing a PNG Image Into a Graphics Context for Blending Mode Manipulation

I need to draw a series of PNGs into a CGContext so I can blend them together:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeOverlay);
Currently I have my app working based on just drawing each image as a UIImageView and adding them as a subview to the view file via: [self addSubview:someImageView] ...but this doesn't allow for blending mode manipulation, right?
Currently the UIImageView vars are being assigned via:
someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image-name.png"]];
So I've tried replacing those with either of these with no luck:
[[UIImage imageNamed:#"image-name.png"] drawInRect:rect blendMode:kCGBlendModeOverlay alpha:1.0];
and this one needs a CGImage which I'm not sure how to make with the PNGs:
CGContextDrawImage(context, rect, someCGImage);
So what am I missing? These aren't being triggered by any interaction, they just need to load/draw when the app loads.
Thanks for any leads. :)
You don't need to be using UIImageViews.
Instead, bracket your drawing between calls to UIGraphicsBeginImageContext and UIGraphicsEndImageContext.
For example (code not tried):
UIImage* uiimage = [UIImage imageNamed:#"image-name.png"];
UIImage* uiimage2 = [UIImage imageNamed:#"other-image-name.png"];
CGSize size = uiimage.size;
UIGraphicsBeginImageContext(size);
[uiimage drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];
[uiimage2 drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];
UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
If you want to use CG calls, call:
CGContextRef context = UIGraphicsGetCurrentContext();
to get the context ref.

create UIImageView with portion of image file

I'm subclassing UIImageView to create a tile-based application. Essentially, I am taking a single image file and breaking it up into pieces, and then assigning the pieces to my tiles (UIImageViews), so that they can be manipulated independently.
What's the best way to grab a portion of an image and use that to draw a UIImageView? I thought about overriding drawRect and using CGAffineTransform, but it seems like there ought to be a simpler way to do this, perhaps by specifying a CGRect to the UIImage that is passed to the UIImageView, but I don't see an API for this.
Here we go:
UIImage* img = [UIImage imageNamed:#"myImage.jpg"];
CGRect imgFrame = CGRectMake(x, y, tileWidth, tileHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], imgFrame);
UIImage* subImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);