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];
Related
use - (nullable UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets) to snapshot from a UIView which contain a AVCaptureVideoPreviewLayer,and then I want to convert the result(UIView *) to UIImage, and display that, but the UIImage is empty all the time.
CGRect frame = CGRectMake(0, 0, 768, 893);
UIView *photo = [self.cameraView resizableSnapshotViewFromRect:frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
UIGraphicsBeginImageContextWithOptions(photo.bounds.size, NO, [[UIScreen mainScreen] scale]);
[photo drawViewHierarchyInRect:photo.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * iv = [[UIImageView alloc] initWithImage:image];
[iv setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[self addSubview:iv];
CALayer * layer = [iv layer];
layer.borderWidth = 2.0f;
layer.borderColor = [[UIColor orangeColor] CGColor];
Did anyone know why that can not get the right UIImage?
Xcode Version 8.2.1 (8C1002)
MacOS Sierra 10.12.3(16D32)
Deployment Target 9.3
iPad iOS 9.3.5
I don't know the Objective-C-version of this, but try to convert it to Obj-C yourself, and add this code:
let context = UIGraphicsGetCurrentContext()
photo.layer.render(in:context!)
Add it after
[photo drawViewHierarchyInRect:photo.bounds afterScreenUpdates:YES];
but before
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
Use the following method (untested):
- (UIImage *)renderViewToImage:(UIView *)view
{
UIImage* image = nil;
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}
And call:
UIImage *img = [self renderViewToImage:photo]
You might want to make it an Category to UIView.
I am trying to take a screen shot of view inside a cell of UITableView but with attached code I can able only to take a screenshot of cell bounds. The problem is that by UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f). I can only make screenshot of rect size and cannot control origin of rect and
rect = [cell bounds]. so please suggest me some idea.
{
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:path];
__block CGRect rect = [cell bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[cell.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Take the screenShot as a normal screenShot and then crop it
-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
Use like this:
UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)];
To get the frame of a particular tableView Cell. Use this:
CGRect myRect = [tableView rectForRowAtIndexPath:0];//example 0,1,indexPath
Hope this helps. Refer to this link link2
- (UIImage *) screenshot {
CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGRect rec = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //set the frame
[self.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
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'm trying to add text to an image but adding a uilabel as subview to a uiimageview.
I already did that but I want to save them as an image;
I'm using render in context but it's not working.
here's my code:
UIImage * img = [UIImage imageNamed:#"IMG_1650.JPG"];
float x = (img.size.width/imageView.frame.size.width) * touchPoint.x;
float y = (img.size.height/imageView.frame.size.height) * touchPoint.y;
CGPoint tpoint = CGPointMake(x, y);
UIFont *font = [UIFont boldSystemFontOfSize:30];
context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(img.size, YES, 0.0);
[[UIColor redColor] set];
for (UIView * view in [imageView subviews]){
[view removeFromSuperview];
}
UILabel * lbl = [[UILabel alloc]init];
[lbl setText:txt];
[lbl setBackgroundColor:[UIColor clearColor]];
CGSize sz = [txt sizeWithFont:lbl.font];
[lbl setFrame:CGRectMake(touchPoint.x, touchPoint.y, sz.width, sz.height)];
lbl.transform = CGAffineTransformMakeRotation( -M_PI/4 );
[imageView addSubview:lbl];
[imageView bringSubviewToFront:lbl];
[imageView setImage:img];
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[lbl.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * nImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(nImg, nil, nil, nil);
Try the code below. It works for me.
UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *bitmap = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
P.S. I think you don't need to renderInContext UILabel layer.
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!