How to convert UIView as UIImage? - iphone

I am taking screenshot image from currentview.but i want to set frameā€¦.don't want to convert fullview as image.I gave frame size..but it takes always
Image as fullview..any help please?
CGRect rectt = CGRectMake(100, 150,150,200);
UIGraphicsBeginImageContext(rectt.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithFrame:rectt];

try to use this subClass:
// code to use the capture:
// in .h : #import "CaptureView.h"
CaptureView *cloneView = [[CaptureView alloc] initWithView:scrollView ];
[scrollView addSubview:cloneView];
with this files code
for CaptureView.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/CALayer.h>
#interface CaptureView : UIView {
#private
UIImage *_imageCapture;
}
#property(nonatomic, retain) UIImage *imageCapture;
// Init
- (id)initWithView:(UIView *)view;
#end
for CaptureView.m:
#import "CaptureView.h"
// Private
#interface CaptureView (/* Private */)
- (void)settingImageFromView:(UIView *)view;
#end
// Public
#implementation CaptureView
#synthesize imageCapture = _imageCapture;
// Standard
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code.
}
return self;
}
// Init
- (id)initWithView:(UIView *)view {
// if ((self = [super initWithFrame:[view frame]])) {
if ((self = [super initWithFrame: CGRectMake(0, 0,150,200)])) {
// Initialization code.
[self settingImageFromView:view];
}
return self;
}
- (void)settingImageFromView:(UIView *)view {
// CGRect rect = [view bounds];
CGRect rect = CGRectMake(100, 150,150,200);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *imageCaptureRect;
imageCaptureRect = UIGraphicsGetImageFromCurrentImageContext();
_imageCapture = imageCaptureRect;
// _imageCapture = UIGraphicsGetImageFromCurrentImageContext();
// [_imageCapture retain];
UIGraphicsEndImageContext();
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGPoint accPoint = CGPointMake(0,0);
[_imageCapture drawAtPoint:accPoint];
}
- (void)dealloc {
[_imageCapture release];
[super dealloc];
}
#end

quartz might help you... look at creating image from part of image....
its not a direct solution to your question, but i think you'll be able to tweak it to fit your needs..

Related

glkview drawinrect delegate method called only once

I am trying to add GLKViewController to my UserInterfaceViewController.I am using the example project from raywenderlich tutorials(http://www.raywenderlich.com/5235/beginning-opengl-es-2-0-with-glkit-part-2)
Here is my code snippet
#import <UIKit/UIKit.h>
#interface UserInterfaceViewController : UIViewController
#end
#implementation UserInterfaceViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIViewController *userInterfaceController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:#"HelloGLKitViewController"];
userInterfaceController.view.frame = CGRectMake(50.0, 100.0, 200.0, 200.0);
[self.view addSubview:userInterfaceController.view];
}
The above code is drawing the context in my UserInterfaceViewController view in the mentioned frame which I am expecting.HelloGLKitViewController is having code to rotate cube in GLKViewControllerDelegate method "update" as mntioned below.but GLKViewControllerDelegate is not getting called when HelloGLKitViewController view to my UserInterfaceViewController view.
#import <GLKit/GLKit.h>
#interface HelloGLKitViewController : GLKViewController
#end
#implementation HelloGLKitViewController
#pragma mark - GLKViewDelegate
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
//glClearColor(_curRed, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
glBindVertexArrayOES(_vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}
#pragma mark - GLKViewControllerDelegate
- (void)update {
if (_increasing) {
_curRed += 1.0 * self.timeSinceLastUpdate;
} else {
_curRed -= 1.0 * self.timeSinceLastUpdate;
}
if (_curRed >= 1.0) {
_curRed = 1.0;
_increasing = NO;
}
if (_curRed <= 0.0) {
_curRed = 0.0;
_increasing = YES;
}
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
_rotation += -90 * self.timeSinceLastUpdate;//90 clockwise -90 anticlickwise
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(0), 1, 0, 0);//GLKMathDegreesToRadians(25) for bending Cube
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_rotation), 0, 1, 0);
// modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, self.view.frame.origin.x, self.view.frame.origin.y,0);
// modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, -self.view.bounds.size.width/2, -self.view.bounds.size.height/2,0);
self.effect.transform.modelviewMatrix = modelViewMatrix;
}
#end
What should I do to call GLKViewControllerDelegate update method.
Problem solved: Here is my code
#class HelloGLKitViewController;
#interface UserInterfaceViewController : UIViewController
{
HelloGLKitViewController *myHLKViewController;
}
#end
#implementation UserInterfaceViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"viewDidLoad of userinterfaceUiViewController");
myHLKViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:#"HelloGLKitViewController"];
[self.view addSubview:myHLKViewController.view];
[self addChildViewController:myHLKViewController];
[myHLKViewController didMoveToParentViewController:self];
}
#end
Thanks for the ref link Using GLKView with a UIViewController

Draw line in UITextView

I want to draw line in my UITextView. After some research I found this:
UITextView ruled line background but wrong line height
I tried in my code, drawRect is called but no line is drawn.. Someone could help me out here ?
#import "FacebookViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface MyViewController (){
UITextView *text;
}
#end
#implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Do any additionnal customisation
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
text.contentMode = UIViewContentModeRedraw;
//TextView init
text = [[UITextView alloc]initWithFrame:CGRectMake(0,44,320,380)];
[self.view addSubview:text];
text.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)drawRect:(CGRect)rect {
NSLog(#"TEST");
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (text.contentSize.height + text.bounds.size.height) / text.font.leading;
//Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
CGFloat baselineOffset = 6.0f;
//iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, text.bounds.origin.x, text.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, text.bounds.size.width, text.font.leading*x + 0.5f + baselineOffset);
}
CGContextClosePath(context);
CGContextStrokePath(context);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[text setNeedsDisplay];
}
#end
Probably a silly mistake but I can't find it out
Thanks
As i looked to you code everything is ok with it. Maybe there is something with color? May be it is the same as background color or something like this.
May be you forgot to set content mode:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.contentMode = UIViewContentModeRedraw;
}
return self;
}
EDIT:
I think i know:
You forgot to set [myTextView setNeedsDisplay]; after showing it. Read here
EDIT:
First you create your view:
t = [[[MyTextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)] autorelease];
[self.view addSubview:t];
t.delegate = self;
Make your viewcontroller implement UITextViewDelegate and after that
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[t setNeedsDisplay];
}
This should work
I have same problem ..
As i think you are not subclassing from UITextView
As UIViewController does not implement this method only UITextView did
So just subclass it .. you code will work
Is it possible that you're drawing successfully but getting obscured by a matching background color? I notice that the alpha on the color you've chosen is pretty small. If only to rule this out, change:
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.5f alpha:0.15f].CGColor);
to
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

Display a progress bar inside a UITextField object

How can one draw a progressbar inside a UITextField ? I have tested two ways so far.
1. Add a UIProgressView object as a subview of the UITextField object.
UIProgressView* progressView = [[UIProgressView alloc] init];
[aUITextField addSubview:progressView];
progressView.progress = 0.5;
[progressView release];
2. Subclass UITextfield and override drawRect:.
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
[[UIColor orangeColor] setFill];
[[UIBezierPath bezierPathWithOvalInRect:rect] fill];
}
Both approaches didn't work. Do you see any problem with these approaches? And how can I make this work?
I am not sure adding the UIProgressView as a subview of a UITextField object will be useful as you can't change the frame of the progress view.
Subclassing seems to be the right approach. Here is what I could come up with. Check if it is useful to you.
ProgressField.h
#interface ProgressField : UITextField {
}
#property (nonatomic, assign) CGFloat progress;
#property (nonatomic, retain) UIColor * progressColor;
#end
ProgressField.m
#implementation ProgressField
#synthesize progress;
#synthesize progressColor;
- (void)setProgress:(CGFloat)aProgress {
if ( aProgress < 0.0 || aProgress > 1.0 ) {
return;
}
progress = aProgress;
CGRect progressRect = CGRectZero;
CGSize progressSize = CGSizeMake(progress * CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
progressRect.size = progressSize;
// Create the background image
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, self.bounds);
CGContextSetFillColorWithColor(context, [self progressColor].CGColor);
CGContextFillRect(context, progressRect);
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[super setBackground:image];
}
- (void)setBackground:(UIImage *)background {
// NO-OP
}
- (UIImage *)background {
return nil;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self setBorderStyle:UITextBorderStyleBezel];
}
return self;
}
This doesn't seem to work with UITextFields with borderStyle set to UITextBorderStyleRoundedRect.
UIProgressView* progressView = [[UIProgressView alloc] init];
progressView.frame = aUITextField.frame;// you can give even set the frame of your own using CGRectMake();
[aUITextField addSubview:progressView];
progressView.progress = 0.5;
[progressView release];
Set the progressview's frame.
Here I think you have to add progressView as subview to self.view , just set progressView's frame according to size that will fit in to UITextField , and make set center of progressview to center of UITextField .
hope it will help you.

UITextView ruled line background but wrong line height

I have a UITextView where the user can create notes and save into a plist file.
I want to be able to show lines just like a normal notebook. The problem I have is
that the text won't align properly.
The image below explains the problem quite well.
This is the background I use to create the lines like the Notes.app
This is my code for creating the background for my UITextView:
textView.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:19.0];
textView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: #"Notes.png"]];
I know that the UIFont.lineHeight property is only available in > iOS 4.x.
So I wonder if there is another solution to my problem?
You should try and draw your lines programmatically rather than using an image. Here's some sample code of how you could accomplish that. You can subclass UITextView and override it's drawRect: method.
NoteView.h
#import <UIKit/UIKit.h>
#interface NoteView : UITextView <UITextViewDelegate> {
}
#end
NoteView.m
#import "NoteView.h"
#implementation NoteView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:0.6f alpha:1.0f];
self.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:19];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;
//Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
CGFloat baselineOffset = 6.0f;
//iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
}
//Close our Path and Stroke (draw) it
CGContextClosePath(context);
CGContextStrokePath(context);
}
#end
MyViewController.h
#import <UIKit/UIKit.h>
#import "NoteView.h"
#interface MyViewController : UIViewController <UITextViewDelegate> {
NoteView *note;
}
#property (nonatomic, retain) NoteView *note;
#end
MyViewController.m
#import "MyViewController.h"
#import "NoteView.h"
#define KEYBOARD_HEIGHT 216
#implementation MyViewController
#synthesize note;
- (void)loadView {
[super loadView];
self.note = [[[NoteView alloc] initWithFrame:self.view.bounds] autorelease];
[self.view addSubview:note];
note.delegate = self;
note.text = #"This is the first line.\nThis is the second line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\n";
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[note setNeedsDisplay];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGRect frame = self.view.bounds;
frame.size.height -= KEYBOARD_HEIGHT;
note.frame = frame;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
note.frame = self.view.bounds;
}
- (void)dealloc {
[note release];
[super dealloc];
}
Take a look at Apple's documentation for Managing the Keyboard, specifically "Moving Content That Is Located Under the Keyboard". It explains how to listen for NSNotifcations and adjust your views properly.
I think the problem is with your image, the yellow space over the line is creating the problem.
You should edit the image.
And nice work.

What is the most efficient way to add a reflection to a UIImageView

I just want the easiest way to make a reflection under a UIImageVies that is easily managable.
Just use the sample code in the the iPhone SDK library
Update: Link now updated to new location
As Phil says, you can have a "reflected" UIImageView instance:
#interface ReflectedImageView : UIView
{
#private
UIImageView *_imageView;
UIImageView *_imageReflectionView;
}
#property (nonatomic, retain) UIImage *image;
#end
And then, in your implementation, something like this
#implementation ReflectedImageView
#dynamic image;
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
self.backgroundColor = [UIColor clearColor];
// This should be the size of your image:
CGRect rect = CGRectMake(0.0, 0.0, 320.0, 290.0);
_imageReflectionView = [[UIImageView alloc] initWithFrame:rect];
_imageReflectionView.contentMode = UIViewContentModeScaleAspectFit;
_imageReflectionView.alpha = 0.4;
_imageReflectionView.transform = CGAffineTransformMake(1, 0, 0, -1, 0, 290.0);
[self addSubview:_imageReflectionView];
_imageView = [[UIImageView alloc] initWithFrame:rect];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:_imageView];
}
return self;
}
- (void)setImage:(UIImage *)newImage
{
_imageView.image = newImage;
_imageReflectionView.image = newImage;
}
- (UIImage *)image
{
return _imageView.image;
}
- (void)dealloc
{
[_imageView release];
[_imageReflectionView release];
[super dealloc];
}
#end
I wrote an article on how to generate reflections of any UI element or group of elements. The canned technique can be dropped into your project and is really easy to use. Source code and the article can be found at http://aptogo.co.uk/2011/08/no-fuss-reflections/
The easiest way is to do it by hand in Photoshop! (seriously - if that's practical just do that).
Otherwise you'll need to make an inverted copy of the image (or at least the bottom half) to place below the real one, overlaid with a gradient from black (assuming your background is black) with alpha=1 to alpha=0.
If you need to place it over arbitrary backgrounds it's a little more complex as you'll have to apply the gradient from alpha = 0 to alpha = 1 to your inverted image.
I knocked up some code once to do it - will try and dig it out later when I get to my Mac, if nobody else has come up with anything sooner.