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;
}
I am trying to center a UIImageView in a scroll view. Here is the code I tried with no results.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kReceiptDetailScrollViewBackgroundCornered]];
[iv setContentMode:UIViewContentModeCenter];
[[self scrollView] insertSubview:iv atIndex:0];
Anybody know what's wrong with this?
You set the image view's content mode to center, which means that the image is not scaled and it can be larger than the image view that displays it.
UIImage *image = [UIImage imageNamed: kReceiptDetailScrollViewBackgroundCornered];
UIImageView *iv = [[UIImageView alloc] initWithImage: image];
iv.contentMode = UIViewContentModeScaleAspectFit;
iv.frame = [self scrollView].bounds;
[[self scrollView] addSubview: iv];
So I have an navigation bar I would like to customize and center no matter what I do I either get the image tiling like so.
Here is the code i am using to set the self.navigation.titleView in my UITableControllerView
self.navigationItem.titleView = [self titleView]; // happens in viewDidLoad
- (UIView *)titleView{
UIImage *headerImage = [UIImage imageNamed:#"header#2x"];
UIImageView *containerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headerImage.size.width, headerImage.size.height)];
return containerView;
}
Thanks.
UIImageView *imgtitle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
[imgtitle setContentMode:UIViewContentModeCenter];
self.navigationItem.titleView = imgtitle;
imgtitle.image = [UIImage imageNamed:#"logo.png"];
I'm trying to create an animated UIImageView with certain image constraints.
I want the UIImageView animating at a size of 141x262, and not the entire screen. Currently I have this code:
CJ = [[UIImageView alloc] initWithFrame:self.view.frame];
CJ.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"CJ_1.png"],
[UIImage imageNamed:#"CJ_2.png"],
[UIImage imageNamed:#"CJ_3.png"], nil];
CJ.animationDuration = 1;
CJ.animationRepeatCount = 0;
[CJ startAnimating];
[self.view addSubview:CJ];
Any and all help will be appreciated!
If you want the view to be a different size, set its frame to be that size:
CJ = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 141, 162)];
If your images are large, you probably also want to set a contentMode on your view. For example:
CJ.contentMode = UIViewContentModeScaleAspectFit;
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.