i have the following code that i mostly got from SO but my image still is not getting cropped, what am i doing wrong?
#synthesize TopImage;
#synthesize BottomImage;
#synthesize RotaterImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
TopImage = [UIImage imageNamed:#"zero.png"];
[TopImage drawAtPoint:CGPointMake(0, 0)];
[self cropImage:TopImage:0];
[self addSubview:[[UIImageView alloc] initWithImage:TopImage]];
// BottomImage = [UIImage imageNamed:#"zero.png"];
// [BottomImage drawAtPoint:CGPointMake(0, 55)];
// [self cropImage:BottomImage:50];
// [self addSubview:[[[UIImageView alloc] initWithImage:BottomImage]retain]];
// RotaterImage = [UIImage imageNamed:#"zero.png"];
// [RotaterImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:0];
// [self addSubview:[[[UIImageView alloc] initWithImage:RotaterImage]retain]];
// [self cropImage:RotaterImage:50];
}
return self;
}
-(void)cropImage:(UIImage *)image:(int)startCroppingPosition{
CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect);
// or use the UIImage wherever you like
image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}
I am very new to IOS so any help is appreciated
You're creating a new cropped image but not telling the init method about it :)
Try this change :
-(UIImage *)cropImage:(UIImage *)image:(int)startCroppingPosition{
CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect);
// or use the UIImage wherever you like
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return newImage;
}
and, in init, replace
[TopImage drawAtPoint:CGPointMake(0, 0)];
with
UIImageView* topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = CGRectMake(x,y,width,height);
Your crop method now returns the newly cropped image and stores it in TopImage.
Other advice :
Class names begin with a CapitalLetter, variables begin with lowercase. TopImage should really be topImage.
Also, I would always use named parameters in your methods, so
-(void)cropImage:(UIImage *)image:(int)startCroppingPosition
should be something like
-(void)cropImage:(UIImage *)image startPosition:(int)startCroppingPosition
That will make your code much much more readable when you come back to it in a month's time (I've learned that the hard way in the past!)
You are only changing the image and not the image of the UIImageView,
What I would do is replace:
[self addSubview:[[UIImageView alloc] initWithImage:TopImage]];
with
UIImageView *iv = [[UIImageView alloc] initWithImage:TopImage]];
iv.tag = 1000 //number does not matter
[self.view addSubview:iv];
[iv release];
then in crop image you just add:
UIImageView *iv = [self.vief viewWithTag:1000];
iv.image = [UIImage imageWithCGImage:imageRef];
This should then work as expected!
Related
Crop image from particular position and set to another view
Final image view
self.finalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)];
if(rectangle_button_preesed_view)
{
self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 120, 260, 340)];
}
else
{
self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 80, 260, 260)];
}
Cropping image
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
NSLog(#" cropped size %f %f ",cropped.size.width,cropped.size.height);
return cropped;
}
#implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)cropRect {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropedImage;
}
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
OR
#implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)rect {
rect = CGRectMake(rect.origin.x*self.scale,
rect.origin.y*self.scale,
rect.size.width*self.scale,
rect.size.height*self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
#end
Beside CoreGraphics solution, I would also suggest to use CoreImage which support filters for image manipulation. You should look at CICrop filter found in this documentation page.
Note: This filter is supported for iOS 5 or greater.
I currently do not have an exact solution but you can go ahead with a similar thread.
Hope it helps!
I want to merge multiple UIImages in iOS.
To do this, I tried to do as below:
UIImage *imageLeft = [UIImage imageNamed:#"ico-left"];
UIImage *imageRight = [UIImage imageNamed:#"ico-right"];
CGSize size = CGSizeMake(imageLeft.size.width + imageRight.size.width, imageLeft.size.height);
UIGraphicsBeginImageContext(size);
[imageLeft drawInRect:CGRectMake(0, 0, imageLeft.size.width, imageLeft.size.height)];
[imageRight drawInRect:CGRectMake(imageLeft.size.width, 0, imageRight.size.width, imageRight.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[cell.contentView addSubview:imageView];
But I cannot get any image. How can I fix it?
Thanks.
I think you are not adding extension of image (.png) so change your upper two lines by these
UIImage *imageLeft = [UIImage imageNamed:#"ico-left.png"];
UIImage *imageRight = [UIImage imageNamed:#"ico-right.png"];
I am not sure whether this is your problem but you can give it a try. Sometimes it worked for me
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"ico-left" ofType:#"png"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"ico-right" ofType:#"png"];
UIImage *imageLeft = [[UIImage alloc] initWithContentsOfFile:path1];
UIImage *imageRight = [[UIImage alloc] initWithContentsOfFile:path2];
Here's the code I use to merge images. (I think I found all or part of it online at some point). Put it in a UIImage category.
// NOTE! this method should only be called from the main thread because of
// UIGraphicsGetImageFromCurrentImageContext();
- (UIImage *)merge:(UIImage *)image atRect:(CGRect)pos overlay:(BOOL)overlay fillColor:(UIColor *)fill
{
UIGraphicsBeginImageContext(self.size);
UIImage *bottom = (overlay)?self:image;
UIImage *top = (overlay)?image:self;
CGRect lf = CGRectMake(0, 0, self.size.width, self.size.height);
CGRect bottomRect = (overlay)?lf:pos;
CGRect topRect = (overlay)?pos:lf;
if (fill){
[fill setFill];
CGContextFillRect (UIGraphicsGetCurrentContext(), topRect);
}
[bottom drawInRect:bottomRect];
[top drawInRect:topRect];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
I have a UITextView and an UIImageView that I want to convert into a single image to share.
SaveImageView, is the ImageView, where I want to save the image of the textview.
The Textview can move it across the screen, so I decide to save their final position and give it to the SaveImageView.
First convert the UItextView in an image and save his position.
Then, I want to join two imageview, into a single image.
self.SaveTextView.frame = self.textView.frame;
UIGraphicsBeginImageContext (self.textView.bounds.size);
[self.textView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.SaveTextView.image=resultingImage;
//join two uiimageview
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.BaseImageView.image drawInRect:CGRectMake(0, 0, self.BaseImageView.frame.size.width, self.BaseImageView.frame.size.height)];
[self.SaveTextView.image drawInRect:CGRectMake(self.SaveTextView.frame.origin.x, self.SaveTextView.frame.origin.y, self.SaveTextView.frame.size.width, self.SaveTextView.frame.size.height)];
_SaveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The problem is that the place where I write in the textview is not in the same position of the saved image, the image with the text is slightly moved, when it should be the exact spot where I have written, not find where is the error.
Any Help ??
Try implement this method and call it on superview:
#implementation UIView (ScreenShot)
- (UIImage *)screenShot
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#end
Try this...
//Make Label
UILabel *_labelTimeOutName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
_labelTimeOutName.text = #"Your label text here";
_labelTimeOutName.minimumScaleFactor = 0.5;
_labelTimeOutName.numberOfLines = 1;
_labelTimeOutName.textColor = [UIColor whiteColor];
_labelTimeOutName.backgroundColor = [UIColor clearColor];
_labelTimeOutName.font = [UIFont fontWithName:#"TrebuchetMS-Bold" size:24];
_labelTimeOutName.textAlignment = NSTextAlignmentCenter;
//add label inside a temp View
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
tempView.backgroundColor = [UIColor clearColor];
[tempView addSubview:_labelTimeOutName];
//render image
//CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(tempView.bounds.size, tempView.opaque, 0.0);
// The view to be rendered
[[tempView layer] renderInContext:UIGraphicsGetCurrentContext() ]; //context];
// Get the rendered image
//////////////////////////////////////////////////////////////////////
UIImage *FinalTextIamge = UIGraphicsGetImageFromCurrentImageContext();
//////////////////////////////////////////////////////////////////////
UIGraphicsEndImageContext();
Enjoy!
The following code splits an image into 2. It seems working fine with non-retina devices, however it gives a different output with retina devices. Could someone please help me fix it? Thanks..
My Code
UIImage *img = [UIImage imageNamed:#"apple.png"];
CGSize sz = [img size];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width/2, sz.height), NO, 0);
[img drawAtPoint:CGPointMake(-sz.width/2, 0)];
UIImage *right = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
rightView = [[[UIImageView alloc] initWithImage:right] autorelease];
rightView.frame = CGRectMake(self.view.frame.size.width/2, 0, self.view.frame.size.width/2, self.view.frame.size.height);
CGImageRef leftRef = CGImageCreateWithImageInRect([img CGImage],CGRectMake(0,0,sz.width/2,sz.height));
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width/2, sz.height), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, CGRectMake(0,0,sz.width/2.0,sz.height), leftRef);
UIImage *left = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *rotatedImage = [left imageRotatedByDegrees:180.0];
leftView = [[[UIImageView alloc] initWithImage:rotatedImage] autorelease];
leftView.frame = CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height);
leftView.transform = CGAffineTransformMake(-1,0,0,1,0,0);
CGImageRelease(leftRef);
[self.view addSubview:leftView];
[self.view addSubview:rightView];
non-retina
retina
PS: I don't know if this is important but apple.png has a #2x version..
The [-UIImage size] property returns the size in points, not in pixels. You probably need to also call [-UIImage scale] to figure out how the image is scaled.
When you create the left view with
leftView = [[[UIImageView alloc] initWithImage:rotatedImage] autorelease];,
you're not specifying the correct scale. Rather than creating your UIImage this way:
UIImage *left = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *rotatedImage = [left imageRotatedByDegrees:180.0];
try creating a CGImageRef and then initializing the UIImage using
[UIImage imageWithCGImage:scale:orientation:]
while specifying the correct scale. There are a number of ways to convert the raw image data from the context to a CGImageRef, or you can use the image you've created and use the CGImage property of UIImage.
I have this code:
CGRect visibleRect;
visibleRect.origin = mainViewController.scrollView.contentOffset;
visibleRect.size = mainViewController.scrollView.bounds.size;
I want to store the content inside visibleRect in a UIImage. Is that possible?
This code may work:
UIGraphicsBeginImageContext(visibleRect.size);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), visibleRect.origin.x,
visibleRect.origin.y);
[mainViewController.scrollView.layer
renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You can't store a UIImage in the CGRect. You'd do something like the following
CGRect visibleRect;
visibleRect.origin = mainViewController.scrollView.contentOffset;
visibleRect.size = mainViewController.scrollView.bounds.size;
UIImageView* img = [[UIImageView alloc] init];
[img setFrame:visibleRect];
[img setImage:[UIImage imageNamed:#"Your-img"]];
[[self view] addSubview:img];
[img release];