Repeating background image for UITextView? - ios

Currently I am doing this to have a background image for my UITextView:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
imgView.image = [UIImage imageNamed:#"background.png"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[imgView release];
But the problem is, it does not repeat. What I want, is the image to repeat after each UIImage.
Is this possible?
Thanks!

textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed#"background"]];
Yes, omit the .png from the image name, it will allow you to provide a Retina version background#2x.png.

Related

Bug about UIImageView display gif?

I create a UIImageView and add to VC UIView , then I create a image with animatedImageWithImages:duration:
When I execute the code, it works normally. It displays image animation. But when I push to next VC and in the process of swipe back , I find the UIImageView display nothing.
At the moment finger left screen , everything back to normal.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
[self.view addSubview:imgView];
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}
BugGif
I notice the imageView has CAKeyframeAnimation. I guess the conflict that runmode and CAKeyframeAnimation.
SDWebImage has the same problem. Because it also use the method that create UIImage. I think the point is the method of animatedImageWithImages:duration: and CAKeyframeAnimation.
Anyone knows the solution? Thanks a lot.
The event that the finger left the screen will make imageView display normally. Why and How to resolve?
GitHubBugDemo
You can use UIImageView another methods to set images and animation duration as below
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];

How can I draw a banner at the top of a UIImageView in Obj C

Some Context
I am creating a feature that enables the users to share some Images from my App to their Facebook/Twitter account.
The feature works, I implemented the IBAction and I can share some Images within Facebook and Twitter.
The Issue
My company's marketing department is now asking me to add at the top of the shared images, a banner with the company's logo and the date.
This is what I have to display :
For now I only share the white rectangle. Now I have to add this banner, the blue rectangle.
My Thoughts
I was wondering how I will be developing it. First of all I was thinking about drawing it programmatically within a view.
UIView *banner = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)];
banner.backgroundColor = [UIColor blueColor];
[self.view addSubview:banner];
The problem is I really don't know how to make it resizable in order to fit all the different screen sizes. But also, how to add the logo within this rectangle, the label date and finally, how can I fit it in my shared UIImage
Then I was wondering, "maybe I can draw it like in Android, directly in the XML file". But from all my investigations, it looks easier to do it programmatically.
Help needed
I would appreciate some advices in order to focus myself on a better way to develop it, because right now my ideas are kind of fuzzy.
Thank you for reading.
You can do it in this way
Consider the img1 is the image which you get from URL.
Here I have taken a bannerView, You can add the label and image into it.
Code :
UIImage *img1 = [UIImage imageNamed:#"Screen.png"];
//Only banner view, on which we will design the label and logo
UIView *vwBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img1.size.width, 60)];
vwBanner.backgroundColor = [UIColor grayColor];
//Logo design
UIImageView *imgVwLogo = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(vwBanner.frame) - 100 , 0, 100, 100)];
imgVwLogo.backgroundColor = [UIColor redColor];
[vwBanner addSubview:imgVwLogo];
//Label design
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, CGRectGetWidth(vwBanner.frame) - imgVwLogo.frame.size.width, CGRectGetHeight(vwBanner.frame))];
lbl.text = #"Thurseday, 20 October 2016";
[vwBanner addSubview:lbl];
//Full view which contains the downloaded image and banner image
UIView *vwWithBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img1.size.width, img1.size.height+vwBanner.frame.size.height)];
[vwWithBanner addSubview:vwBanner];
//imageView with downloaded image
UIImageView *imgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(vwBanner.frame), img1.size.width, img1.size.height)];
imgVw.image = img1;
[vwWithBanner addSubview:imgVw];
//Draw the image of full view
UIGraphicsBeginImageContextWithOptions(vwWithBanner.bounds.size, NO, img1.scale);
[vwWithBanner drawViewHierarchyInRect:vwWithBanner.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Output :
See the gray bar is the Banner View and the below is the image. I did not have image so taken a screen shot of simulator to demonstrate the example.
UIView *banner = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)];
banner.backgroundColor = [UIColor blueColor];
UIImageView *imgBanner=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, banner.frame.size.width, banner.frame.size.height)];
[imgBanner setImage:#"img.png"];
imgBanner.contentMode = UIViewContentModeScaleAspectFit;
[banner addSubView:imgBanner];
[self.view addSubview:banner];
Call this method once you get the image,
UIImage *img = [self drawText:#"Some text"
inImage:img
atPoint:CGPointMake(0, 0)];
Function implementation like this,
+(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIFont *font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

How to set the image in Background image in UIViewController in ios

How to set the image in Background image(means how to paste the image in Background image). I took one image as a background image in UIViewController. Right now i want to put the another image in background image.i know how to put the image as a Background image in UIViewController.But i have no idea how to put the image in background image.I tried the code.But the image will not displayed in background image.This is my code.please help me any body.Thanks in advance.
-(void)viewDidLoad
{
backgroundImage=[UIImage imageNamed:#"bg-small.PNG"];
audioViewBackgroundImage=[[UIImageView alloc]initWithImage:backgroundImage];
[self.view addSubview:audioViewBackgroundImage];
mImage=[UIImage imageNamed:#"muluguphoto.png"];
mBackgroundImage=[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage=[[UIImageView alloc]initWithImage:mImage];
[audioViewBackgroundImage addSubview:mBackgroundImage];
}
add it to your viewDidLoad..
UIImageView *newImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newImage setBackgroundColor:[UIColor redColor]];//here else you can add image
[self.view addSubview:newImage];
UITableView *newTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newTable setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:newTable];
you have to set imageview above imageview in userinterface.like in photo.make proper heirARCHY!and set frame of imageview INDependentaly as you want.here is background image with logo image..
Add this line to viewDidLoad
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Why do you initializing mBackgroundImage two times?
Let's try the below code and let me know the result.
UIImageView * audioViewBackgroundImage=[[UIImageView alloc] initWithFrame:self.view.frame];
audioViewBackgroundImage.image = [UIImage imageNamed:#"bg-small.PNG"];
[self.view addSubview:audioViewBackgroundImage];
UIImageView * mBackgroundImage =[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage.image = [UIImage imageNamed:#"muluguphoto.png"];
[audioViewBackgroundImage addSubview:mBackgroundImage];
Thanks!
UIImageView * bgImage =[[UIImageView alloc]initWithFrame:self.view.frame];
bgImage.image = [UIImage imageNamed:#"myimg.png"];
[self.view addSubview:bgImage];
[self.view sendSubviewToBack:bgImage];
//write this line in viewdidload and make sure that you have import the image in your project by copying it .
when you write this line of code then this image will automatically replace the previous background image
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"stack.jpeg"]];

merging uilabel and uiimageview

I picked an image from the photo album and put it in an UIImageView called imageView
I then added some text over the image using UITextView as source of input and a UILabel on top of the uiimageview to display the text.
So far so good.
Now I'm trying to merge the label and the imageview in order to do other stuff with that but I 'm not getting it to work. I have tried some solutions given to similar questions but they didn't work for me.
I tried this
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[_displaylabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
it tells me that Received type "CAlayer for instance message is a forward declaration".
any other suggestions?
Try this:
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Make sure that the UILabel is a subview of your UIImageView, so that rendering the layer will include all sublayers (the UILabel included). It seems like you forgot to include UIGraphicsEndImageContext(); too, which may have been the issue
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
[tempView addSubview:imgView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(tempView.frame.size.width/2, 0, 200, tempView.frame.size.height/2)];
[label setText:#"Hello... You there"];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[tempView addSubview:label];
UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

setting an image in background of a label programmatically

I have a label (many labels in fact) now I want an image in the background of my label (same image) so that text writtenin my label is shown to user. How to do it programmatically?
The simplest way would just be to simply layer a UIImageView behind the UILabel. If you wanted to make it more robust you could make a UILabel subclass that exposes a method or property to set the image background and it would add the image to itself.
CGPoint labelPos = CGPointMake(123, 234);
UIImage *theImage = [UIImage imageNamed:#"button_bg.png"];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:theImage];
theImageView.frame = CGRectMake(labelPos.x, labelPos.y, theImage.size.width, theImage.size.height);
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelPos.x, labelPos.y, 100, 50)];
theLabel.backgroundColor = [UIColor clearColor];
// ...label config...
[self.view addSubview:theImageView];
[self.view addSubview:theLabel];
[theImageView release];
[theLabel release];

Resources