Set Background Image to UILabel Properly in iOS - ios

So How you will set a background image to UILabel.
Here is the ways i have tried after googling and reading some of Stackoverflow posts.
I have a image and want to fit / shrink according to size of UILabel.
1 --
lblDelivery.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Tick.png"]];
and result is :
2 --
UIImage *img = [UIImage imageNamed:#"Tick.png"];
CGSize imgSize = lblDelivery.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lblDelivery.backgroundColor = [UIColor colorWithPatternImage:newImage];
and result is :
Please help on this issue. how to i can get this image in BG perfectly

You may use a custom button rather than label having no action against button.
For that you can use this piece of code to set the background image, and it will be stratched automatically.
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
Although There is no action against this button, you may also disable user intraction on this button as
yourButton.userInteractionEnabled = NO;

Related

images in Collection View are bad quality

I have a collection view with a bunch of images in it but the problem is that the images are very blurry and pixelated in the collection view. When I select one of the pictures, it shows a larger picture that makes it good enough quality to see. So I'm guessing its the scaling of the picture into a small cell that makes it bad quality in the collection view. How do I go about fixing the images so they don't look blurry/bad quality in the collection view?
The actual picture's size 640x853 and the cell size is 100x133.
this is what I have:
UIImage *theImage = [UIImage imageWithData:data];
UIGraphicsBeginImageContext(CGSizeMake( cell.frame.size.width, cell.frame.size.height));
[theImage drawInRect: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
UIImage *small = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:small];
cell.backgroundColor = [UIColor whiteColor];
cell.clipsToBounds = YES;
[cell.contentView addSubview:cellImageView];

I want to move to the left edge of the cell, the image

I was displaying an image on UITableviewcell.
I'm getting in the URL image.
I'm getting asynchronously image.
There is a blank space on the left side of the image.
I want to remove the margin on the left side of the image.
Thanks.
It may be because of UITableView . Try like this
if ([table respondsToSelector:#selector(setSeparatorInset:)]) {
[table setSeparatorInset:UIEdgeInsetsZero];
}
step 1
Create a custom UITableViewCell
step 2
place the image View on one edge
step 3
since the picture is coming asynchronously, there may be a problem of getting actual image size. to avoid this prob.
scale the image in the size you want it to be.
using this snippet
-(UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
You can do something like this if space is not part of image..
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
img.image = [UIImage imageNamed:#"one.png"];
[cell addSubview:img];

iOS 7 Notification Center Like Label

In the iOS7 notification centre, the labels (and the separator lines) have a very interesting background: the blur image, and what looks like the soft light blend mode.
I'm unsure what to search for. A pointer as to how this could be done would be really appreciated.
Till now, I've tried to replicate the effect by setting a part of the blurred image as the background using label.textColor = [UIColor colorWithPatternImage:...]. This also doesn't account for the case when the background is all black (or white), and leads to the text becoming unreadable.
But that doesn't seem to work just right.
Like this:
Here's what I've tried:
- (void)viewDidLoad
{
[super viewDidLoad];
const CGFloat fontSize = 25.f;
const NSString *text = #"A long-ish string";
CGSize size = [text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"Avenir Next" size:fontSize]}];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 270, size.width, size.height)];
label.font = [UIFont fontWithName:#"Avenir Next" size:fontSize];
label.textAlignment = NSTextAlignmentNatural;
label.backgroundColor = [UIColor clearColor];
label.text = text;
UIImage *image = [UIImage imageNamed:#"wat#2x"];
UIImage *blurredImage = [image applyBlurWithRadius:20.5 tintColor:[UIColor clearColor] saturationDeltaFactor:1.f maskImage:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[blurredImage applyDarkEffect]];
imageView.frame = self.view.bounds;
CGFloat imgScale = image.scale;
CGRect labelFrame = label.frame;
CGRect realRect = CGRectMake(labelFrame.origin.x * imgScale, labelFrame.origin.y * imgScale, labelFrame.size.width * imgScale, labelFrame.size.height * 2.0);
CGImageRef labelPatternImage = CGImageCreateWithImageInRect(image.CGImage, realRect);
label.textColor = [UIColor colorWithPatternImage:[UIImage imageWithCGImage:labelPatternImage scale:2.f orientation:UIImageOrientationUp]];
CGImageRelease(labelPatternImage);
[self.view addSubview:imageView];
[self.view addSubview:label];
}
This code results in
Code Result http://caughtinflux.com/static/result.png
As you can see, that isn't similar to the NC label.
EDIT
The blurred image background for the text should align with the actual background as much as possible. Hopefully the simulator screenshot of my code helps make sense of what I'm saying.
This is a blur effect. You can find Apple's category on UIImage with this effect available for download here. The files name is UIImage+ImageEffects.h/UIImage+ImageEffects.m any you can use it like that:
UIImage *backgImage = [image applyBlurWithRadius:2
tintColor:tintColor saturationDeltaFactor:0.8 maskImage:nil];
//Extended
You can create your view with the labels on it with lets say white text colour (to highlight in when you will blur whole view) and after that you can create snapshot of of this view and set up it as a background of the view you can use (by [self.view setBackgroundColor:[UIColor colorWithPatternImage:blurredImage];).
UIView *snapshotView = [YOURUIVIEW resizableSnapshotViewFromRect:self.contentView.frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
UIGraphicsBeginImageContextWithOptions( self.contentView.bounds.size, YES, 0.0f);
BOOL result = [snapshotView drawViewHierarchyInRect:self.contentView.bounds
afterScreenUpdates:YES];
UIImage *snapshotImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (result){
UIColor *tintColor = [UIColor colorWithWhite:0.97 alpha:0.82];
UIImage *blurredImage = [snapshotImage applyBlurWithRadius:4 tintColor:tintColor saturationDeltaFactor:1.8
maskImage:nil];
}
Let me know is it something you need. If it doesn't can you explain again, with more details, what you want to achieve?
Did you try adjusting the labels alpha value yet (as easy as it sounds)?
You could try that, and maybe add a bit of white to the blur before applying it to the label.
I have a sample project with notification center like separators, selected background views and (as of today) labels.
You can have a look at it here: https://github.com/mikrohard/BluredTableView
There may still be some bugs, perfomance issues, etc. But feel free to use and improve it.

Create 1 image from masked image in ios

I have a image and i mask this image with another image to make shape.
I just want to change the view background color of Masked image.
I am using [UIColor colorWithPatternImage:maskedImage];
But its not working.
Please suggest me how to merge or create Masked 2 images in 1 image so colorWithPatternImage will work.
UIImage *originalImage = [UIImage imageNamed:#"original.png"]; //my background image
UIImage *maskedImage = [UIImage imageNamed:#"maskedImage.png"]; //my masked image
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
[originalImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[maskedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.6];
UIImage *newMaskedBackGroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
then use this newMaskedBackGroundImage,
ex. [UIColor colorWithPatternImage:newMaskedBackGroundImage];
Are you assigning the value of
[UIColor colorWithPatternImage:maskedImage];
to anything? Like...
self.maskedView.backgroundColor = [UIColor colorWithPatternImage:maskedImage];

Create UIImage from 2 UIImages and label

I got one big UIImage. Over this UIImage i got one more, witch is actually a mask. And one more - i got UILabel over this mask! Witch is text for the picture.
I want to combine all this parts in one UIImage to save it to Camera Roll!
How should I do it?
UPD. How should i add UITextView?
i found:
[[myTextView layer] renderInContext:UIGraphicsGetCurrentContext()];
But this method doesn't place myTextView on the right place.
create two UIImage objects and one UILabel objects then use drawInRect: method
//create image 1
UIImage *img1 = [UIImage imageNamed:#"image1.png"];
//create image 2
UIImage *img2 = [UIImage imageNamed:#"image2.png"];
// create label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50,50 )];
//set you label text
[label setText:#"Hello"];
// use UIGraphicsBeginImageContext() to draw them on top of each other
//start drawing
UIGraphicsBeginImageContext(img1.size);
//draw image1
[img1 drawInRect:CGRectMake(0, 0, img1.size.width, img1.size.height)];
//draw image2
[img2 drawInRect:CGRectMake((img1.size.width - img2.size.width) /2, (img1.size.height- img2.size.height)/2, img2.size.width, img2.size.height)];
//draw label
[label drawTextInRect:CGRectMake((img1.size.width - label.frame.size.width)/2, (img1.size.height - label.frame.size.height)/2, label.frame.size.width, label.frame.size.height)];
//get the final image
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The resultImage which is UIImage contains all of your images and labels as one image. After that you can save it where ever you want.
Hope helps...

Resources