This question already has answers here:
Capture UIView and Save as Image
(7 answers)
Closed 6 years ago.
I have a UIView that has another UIView and a UIImageView inside of it. I want to flaten the UIImage onto the UIView. If it helps you can think of the layout like this:
-UIView
---UIImageView
---UIView
How can I capture the whole UIView and save it as a single image?
You can use this to save your views as a single image.
-(UIImage *)visibleImage
{
UIGraphicsBeginImageContextWithOptions(yourview.frame.size, YES, [UIScreen mainScreen].scale);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), CGRectGetMinX(yourview.frame) , CGRectGetMinY(yourview.frame));
[yourview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img_FinalVisible = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img_FinalVisible;
}
Related
I'm developing painting app in this app I'm using ACEDrawing tool to draw over the view. But I've to cut particular portion of the painting and to paste at anywhere on the view.
I can copy the paintings of the view as an image. my code is here.
In .h file
#property (strong,nonatomic)ACEDrawingView *DrawingViews;
#property (nonatomic, strong)UIScrollView *myScrollView;
in .m file
//creating the Drawing view
_DrawingViews = [[ACEDrawingView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.testView.frame.size.width, self.testView.frame.size.height)];
_DrawingViews.drawTool=ACEDrawingToolTypePen;
_DrawingViews.tag=i;
_DrawingViews.delegate=self;
[myScrollView addSubview:_DrawingViews];
After Inside Copy method.
-(void)copy:(id)sender{
ACEDrawingView *dragView = [self.myScrollView viewWithTag:currentTag];// Getting ACEDrawing View with tag
CGSize size = [_userResizableView1 bounds].size;//just for size
UIGraphicsBeginImageContext(size);
[[dragView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRef context=UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(200,200, 145, 150)];
UIGraphicsEndImageContext();
}
But, I can paste it only as an image using UIImageView. I don't know how to paste it as a painting overview then only I can erase or redraw.
-Gratefull to Mr.Duncan sir and all who will answer.
UIImage has various drawing methods like drawInRect. You've already figured out how to create a graphics context. You could use drawInRect To draw your image into the graphics context.
(Note that you want to use the longer form of begin image context that takes a scale and options. (I'm not at my Mac to look up the function name.) If you pass in a scale of 0 it creates a Retina context on Retina devices. With the code you're using it will degrade retina images to non-retina resolution.)
This question already has answers here:
How can I produce an effect similar to the iOS 7 blur view?
(12 answers)
Closed 8 years ago.
I have a UIView that partially cover some contents and I would like apply a blur effect exactly like the iOS Notification Center.
I'm not looking for a way to apply blur on UIImage, but on UIView with other contents (UIView, UIButton, ecc) under it.
Ideas ?
Thanks.
First change the UIView into an UIImage.
You can use the following as a category of UIView.
#implementation UIView (ConvertToUIImage)
- (UIImage *)asImage {
NSAssert(self, #"Do not use this method on nil instance of UIView.");
if (!self) return 0;
UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 0);
[self drawViewHierarchyInRect:self.frame afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSAssert(image, #"Image must not be nil at this point.");
return image;
}
#end
Next you need to blur the UIImage, there is a related question here Creating a blur effect in iOS7
Finally, use the UIImage as a background.
myView.backgroundColor = [UIColor clearColor];
UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:myView.frame];
bgToolbar.barStyle = UIBarStyleDefault;
[myView.superview insertSubview:bgToolbar belowSubview:myView];
just past from an other answer how ever i use toolbar in my projects and works perfect, i am using alpha=0.98
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The requirement is capture the on-screen content displayed as a UIImage, and then perform a custom dismiss animation on that image.
This piece of code will get you a UIImage of the passed view. To get a image of the whole screen, pass self.view inside some UIViewController of yours
- (UIImage*)screenShotOfView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
The first one, captureView, returns a UIImage that contains a render of any UIView (except EAGLView, see below). If you pass your app’s UIWindow into this method, it will return you a screenshot of the app.
The second method, saveScreenshotToPhotosAlbum, takes it a step further and saves an image that contains a render of any UIView to your iPhone’s photo album.
- (UIImage*)captureView:(UIView *)view
{
CGRect rect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
(void)saveScreenshotToPhotosAlbum:(UIView *)view
{
UIImageWriteToSavedPhotosAlbum([self captureView:view], nil, nil, nil);
}
Press Command + Shift + 4 then select the simulator and snapshot will be saved at your desktop, then drag the saved image to the simulator and will open on safari ... long tab to save image ... and now u can get the image from photos library on simulator.
I have a textbox, where i want the written text to be added to a UIImage.
How can i draw NSString to a UIImage?
I´ve searched, and found lots of examples, but non of them works. Xcode just gives me lots of errors.
Simply put, i want to draw a NSString to a UIimage. The UIImage should be the same size as a predefined UIImageView, and be placed in center.
Any ideas?
i think solution is here..i have used this method in one of my application
here lbltext is the object of my label. this method creates new image with given text & return the object of new image with text.
-(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point
{
UIFont *font = lblText.font;
UIGraphicsBeginImageContext(iv.frame.size);
[image drawInRect:CGRectMake(0,0,iv.frame.size.width,iv.frame.size.height)];
CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);
[lblText.textColor set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
in your below comment you have passed iv.center as a point.. try manualy like this
CGPoint point;
point.x=30; // dynamicaly just for example lblText.frame.origin.x;
point.y=40; //lblText.frame.origin.y;
you have to call above method like this...
UIImage *img = [self drawText:#"Test String" inImage:originalImage atPoint:point];
here,img is another instace of UIImage for storing new image with text...after calling this method use img imager for your further use..
for example...
[newimageviewObj setImage:img];
i hope this will help you
UIImage is not a subview of UIView, so you cant add a subview to it. Also NSString is not a subview of UIView. If you want to show things on the screen, they should inherit from UIView.
So try this:
Create a UIImageView - set its image property to be your UIImage instance.
Create a UILabel - set its text property to your NSString.
Add the UILabel as a subview of your UIImageView.
and finally add your UIImageView to your current view controllers view.
Emm, here is some thoughts.
I think that one simple way is like this :
Put aUIImageView on aView;
Add aUITextView on aView;
Get ScreenShot from aView;
This may works fine.
Also, this may come with a problem that screenshot may be not clear.
Then, After step1 and step2, we may get new image by UIGraphics.
(With here, we already know position where text should be on image and this should be ok.)
PS: Some image may not have some attribution like CGImage and CGImage is needed for this. So, transform it.
I'm creating an app where within a UIView subclass I have several UIImageView's holding UIImage's that the user has picked. I want to be able to allow the user to save the UIView to the PhotoLibrary so that those UIImageView's are saved as a sort of collage within this UIView. Would I need to have a UIImageView in place of the UIView to allow the code to save the whole area? or would I be able to use the UIView?
Thank you in advance!
This function does what you want except storage:
-(UIImage *)takeScreeShotOfView:(UIView *)view
{
UIGraphicsBeginImageContext(CGSizeMake(1024, 748));
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}