UIImage not being allocated any memory using UIGraphicsGetImageFromCurrentImageContext - iphone

I have made a program for image effects but when I try to add the effects like photoshop using the code below the image is not allocated any memory and therefore the resulting image is not shown.
UIImage *bottomImage = originalImage;
UIImage *upperImage = originalImage;
UIGraphicsBeginImageContextWithOptions(sizeR, YES, imageBeforeEffect.scale);
[bottomImage drawInRect:CGRectMake(0.0, 0.0, sizeR.width, sizeR.height)];
[upperImage drawInRect:CGRectMake(0.0, 0.0, sizeR.width, sizeR.height) blendMode:kCGBlendModeDarken alpha:1.0];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Check if your sizeR has got any frame... put a break - point and check it. If there is no frame then the UIGraphicsBeginImageContextWithOptions will not work.

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/

Draw on a UIImage

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.

Capturing CGRect does not give proper image. What to do?

I am capturing CGRect with following code. But the resulting image is not the image what i want. Image has some transparent background. What to do for removing transparent background as suggesting the picture.
- (UIImage *)captureScreenInRect:(CGRect)captureFrame {
CALayer *layer;
layer = imageScrollview.layer;
UIGraphicsBeginImageContext(imageScrollview.bounds.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
\[layer renderInContext:UIGraphicsGetCurrentContext()\];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
}
Translate your context so that its origin matches your captureFrame:
UIGraphicsBeginImageContext(imageScrollview.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, -captureFrame.origin.x, -captureFrame.origin.y);
[imageScrollView.layer renderInContext:c];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
(written from memory, untested)
Additionally clipping the context is not necessary as the image is already clipped by the image context's bounds.
Try this one
CGRect cropRect = CGRectMake(imageScrollview.frame.origin.x+15, imageScrollview.frame.origin.y+15, WIDTH, HEIGHT);

Erase a rect of an UIImageView

I want to know, how can I erase a custom rect (with, for example, an UIView in IB or something else) of an UIImageView in order to display an other UIImageView positioned underneath.
I didn't manage to do it using some response in the forum...
This will clear a rect in an image:
- (UIImage *)clearRect:(CGRect)rect inImage:(UIImage *)image {
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions([image size], NO, 0.0);
else
UIGraphicsBeginImageContext([image size]);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0.0, 0.0, [image size].width, [image size].height)];
CGContextClearRect(context, rect);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
Just load your image and clear the rects before assigning it to the image view:
UIImage *image = [UIImage imageNamed:#"Image.png"];
UIImage *maskedImage = [self clearRect:CGRectMake(10.0, 10.0, 50.0, 50.0) inImage:image];
[imageView setImage:maskedImage];
probably not the best solution but you can do the other way and take the 4 parts around the rect separately and combine them afterwards without the inner rect. You would repeat this as long as you have rect's to crop out.
You cannot clear the UIImageView itself because this just draws the UIImage. So you have erase the rect in the UIImage. Create a bitmap context, draw the image into it. Erase the part you want to "see through" with CGContextClearRect. When create a new new image from the bitmap context.

rotate UIImage and nothing displays

I used the codes below to rotate an UIImage
double angle = -90;
NSData *imageData = [NSData dataWithContentsOfFile:sss];
UIImage *image =[UIImage imageWithData:imageData];
CGSize s = {image.size.width, image.size.height};
UIGraphicsBeginImageContext(s);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0,image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextRotateCTM(ctx, 2*M_PI*angle/360);
CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[myUIImageView setImage:newImage];
but nothing displays on myUIImageView.
If there is anything wrong?
Welcome any comment
Thanks
interdev
If you're going to be rotating the image 90 degrees, you should swap the width and height when you create the image context:
CGSize s = {image.size.height, image.size.width};
This way, there's enough space to draw the fully-transformed image. Attempting to draw an image into a rectangle which exceeds the bounds of a graphics context will just result in nothing being drawn.
P.S. I recommend using a CGBitmapContext instead of UIGraphicsBeginImageContext(), as the former is thread-safe, which means you'd be able to do your image manipulations on a background thread if you so chose.