merging uilabel and uiimageview - ios

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();

Related

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;
}

Sub Views are not visible after change the UILabel Background colour

I have changed my UILabel background colour like this. Actually I have added an image to it. I did it like this.
self.lblMsg=[[UILabel alloc] initWithFrame:CGRectMake(8.0, self.lblDateTime.frame.origin.y+self.lblDateTime.frame.size.height, size.width-5.0, 100.0)];
UIImage *img = [UIImage imageNamed:#"msgBackground"];
CGSize imgSize = self.lblMsg.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.lblMsg.backgroundColor = [UIColor colorWithPatternImage:newImage];
[self.contentView addSubview:self.lblMsg];
Now I want to add another UILabelon to this and that label should include an image as an attachment. So I did it like this.
self.lblWeatherTitle=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.lblMsg.frame.size.width, 50.0)];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:#"splashLogo"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text"];
[self.lblWeatherTitle setTextColor:[UIColor redColor]];
[myString appendAttributedString:attachmentString];
self.lblWeatherTitle.attributedText = myString;
[self.lblMsg addSubview:self.lblWeatherTitle];
But My 2nd UILabel is not visible.
This is what I need to do
This black text is a long description. As the title of it it should show those red label and the yellow icon. so I added that top label as a subview to this white label. This whole thing is inside a custom UITableViewCell
Please help me. How can I do this in correct way?
Thanks

Stop TableView cell UIImage resizing in iOS

I have a standard (i.e. not a custom) TableView cell with text and images, the problem is my images are different sizes and I want to put them in as something like UIViewContentModeScaleAspectFill. I simply want the UIImage to be 54x54 and not resizes all the time - I just want them square!
There are lots of ideas out there but none of them work, the closest I came worked BUT the image was on top of the text (it added another image as a subview).
I am not keen on using a custom cell as I use edit mode to drag stuff about and I have not figured that out yet with custom cells.
EDIT
I latest I have tried;
UIImage *tempImage = [UIImage imageWithData:data];
CGSize size = CGSizeMake(54, 54);
UIGraphicsBeginImageContext(size);
[tempImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = scaledImage;
I have this in the cellForRow which I hoped would do it;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
This only works on custom cells;
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,54,54);
}
This works well but it covers the text;
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:8.0f];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:[UIImage imageWithData: imageData]];
[cell.contentView addSubview:imgView];
This shows the exact problem I am having;
Stackoverflow problem
ANOTHER EDIT
okay this worked, thanks;
[cell setIndentationWidth:54];
[cell setIndentationLevel:1];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:myImages[indexPath.row]];
[cell.contentView addSubview:imgView];
I recommend you to subclass UITableViewCell and create your own custom cell :D
but, anyway... try by adding this
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIImageView *imgView = nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.tag = 123;
[cell.contentView insertSubview:imgView atIndex:0];
[cell setIndentationWidth:54];
[cell setIndentationLevel:1];
}
else
{
imgView = (UIImageView*)[cell.contentView viewWithTag:123];
}
imgView.image = [UIImage imageNamed:#"myImage"];

Centering a UIImageView inside a UIScrollView

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];

Repeating background image for UITextView?

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.

Resources