UIView image background - ios

In my application, I want to set the background image on UIView. In the simulator iOS this code works. And on the iPhone black background. Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"appBG.png"] drawInRect:self.view.bounds];
UIImage *appbg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:appbg];
}
UPDATE: I decided my problem

its better u will take an ImageView on UIView and set the image to it. Rather than setting the image to UIView.

Try this code:
UIImage* backgroundImage = [UIImage imageNamed:#"appBG.png"];
CALayer* aLayer = [CALayer layer];
CGFloat nativeWidth = CGImageGetWidth(backgroundImage.CGImage);
CGFloat nativeHeight = CGImageGetHeight(backgroundImage.CGImage);
CGRect startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight);
aLayer.contents = (id)backgroundImage.CGImage;
aLayer.frame = startFrame;
[self.view.layer addSublayer:aLayer];
[aLayer setNeedsDisplay];
Adjust startFrame value according to your requirement.

Try with Replace following line:
[[UIImage imageNamed:#"appBG.png"] drawInRect:self.view.bounds];
With following line:
[[UIImage imageNamed:#"appBG.png"] drawInRect:self.view.frame];
May be the issue of frame or bound.

Related

Can not convert UIView to UIImage on NewPad 9.3.5

use - (nullable UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets) to snapshot from a UIView which contain a AVCaptureVideoPreviewLayer,and then I want to convert the result(UIView *) to UIImage, and display that, but the UIImage is empty all the time.
CGRect frame = CGRectMake(0, 0, 768, 893);
UIView *photo = [self.cameraView resizableSnapshotViewFromRect:frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
UIGraphicsBeginImageContextWithOptions(photo.bounds.size, NO, [[UIScreen mainScreen] scale]);
[photo drawViewHierarchyInRect:photo.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * iv = [[UIImageView alloc] initWithImage:image];
[iv setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[self addSubview:iv];
CALayer * layer = [iv layer];
layer.borderWidth = 2.0f;
layer.borderColor = [[UIColor orangeColor] CGColor];
Did anyone know why that can not get the right UIImage?
Xcode Version 8.2.1 (8C1002)
MacOS Sierra 10.12.3(16D32)
Deployment Target 9.3
iPad iOS 9.3.5
I don't know the Objective-C-version of this, but try to convert it to Obj-C yourself, and add this code:
let context = UIGraphicsGetCurrentContext()
photo.layer.render(in:context!)
Add it after
[photo drawViewHierarchyInRect:photo.bounds afterScreenUpdates:YES];
but before
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
Use the following method (untested):
- (UIImage *)renderViewToImage:(UIView *)view
{
UIImage* image = nil;
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}
And call:
UIImage *img = [self renderViewToImage:photo]
You might want to make it an Category to UIView.

View lose cornerRadius with Animation of UIViewAnimationOptionTransitionFlipFromLeft on iOS 8

In the code, I have set:
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = imageView.frame.size.width/2;
And run it in animation block:
[UIView transitionWithView:imageView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:nil
completion:^(BOOL finished) {
imageView.hidden = YES;
}];
But when running on iOS 8, the imageView is back to normal, means there is no cornerRadius for imageView.
Could anyone tell me why?
You need to slash corners of UIImage not UIImageView using CALayer using below code.
// load image on imageView programmatically
UIImage *image = [UIImage imageNamed:#"yourImage.png"];
// assign the changes reflected using method before load image to imageView
image = [self makeRoundedImage:image radius:_imageView.frame.size.width/2];
_imageView.image = image;
// method to create round image
-(UIImage *)makeRoundedImage:(UIImage *) image radius: (float) radius{
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
UIGraphicsBeginImageContext(image.size);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
Now you can apply animation simply.

Convert UITextView into an image

I have a UITextView and an UIImageView that I want to convert into a single image to share.
SaveImageView, is the ImageView, where I want to save the image of the textview.
The Textview can move it across the screen, so I decide to save their final position and give it to the SaveImageView.
First convert the UItextView in an image and save his position.
Then, I want to join two imageview, into a single image.
self.SaveTextView.frame = self.textView.frame;
UIGraphicsBeginImageContext (self.textView.bounds.size);
[self.textView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.SaveTextView.image=resultingImage;
//join two uiimageview
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.BaseImageView.image drawInRect:CGRectMake(0, 0, self.BaseImageView.frame.size.width, self.BaseImageView.frame.size.height)];
[self.SaveTextView.image drawInRect:CGRectMake(self.SaveTextView.frame.origin.x, self.SaveTextView.frame.origin.y, self.SaveTextView.frame.size.width, self.SaveTextView.frame.size.height)];
_SaveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The problem is that the place where I write in the textview is not in the same position of the saved image, the image with the text is slightly moved, when it should be the exact spot where I have written, not find where is the error.
Any Help ??
Try implement this method and call it on superview:
#implementation UIView (ScreenShot)
- (UIImage *)screenShot
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#end
Try this...
//Make Label
UILabel *_labelTimeOutName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
_labelTimeOutName.text = #"Your label text here";
_labelTimeOutName.minimumScaleFactor = 0.5;
_labelTimeOutName.numberOfLines = 1;
_labelTimeOutName.textColor = [UIColor whiteColor];
_labelTimeOutName.backgroundColor = [UIColor clearColor];
_labelTimeOutName.font = [UIFont fontWithName:#"TrebuchetMS-Bold" size:24];
_labelTimeOutName.textAlignment = NSTextAlignmentCenter;
//add label inside a temp View
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
tempView.backgroundColor = [UIColor clearColor];
[tempView addSubview:_labelTimeOutName];
//render image
//CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(tempView.bounds.size, tempView.opaque, 0.0);
// The view to be rendered
[[tempView layer] renderInContext:UIGraphicsGetCurrentContext() ]; //context];
// Get the rendered image
//////////////////////////////////////////////////////////////////////
UIImage *FinalTextIamge = UIGraphicsGetImageFromCurrentImageContext();
//////////////////////////////////////////////////////////////////////
UIGraphicsEndImageContext();
Enjoy!

CALayer's image not showing up

I am trying to use CALayer to display an image, but the image is not showing up. I've verified that 'image' is not nil, image.size is correct, and layer.contents is a valid CABackingStore.
- (void)viewDidLoad
{
[super viewDidLoad];
layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
UIImage *image = [UIImage imageNamed:#"download.jpeg"];
layer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
layer.position = self.view.center;
layer.contents = (id)image.CGImage;
[layer setNeedsDisplay];
}
What could be the problem here?
Removing [layer setNeedsDisplay]; should correct the behavior.
From the docs (emphasis added):
Calling this method causes the layer to recache its content. This results in the layer potentially calling either the displayLayer: or drawLayer:inContext: method of its delegate. The existing content in the layer’s contents property is removed to make way for the new content.
either use this
layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
//UIImage *image = [UIImage imageNamed:#"download.jpeg"];
layer.bounds = CGRectMake(0, 0, 300,300);
layer.position = self.view.center;
//layer.contents = (id)image.CGImage;
layer.contents = (id)[UIImage imageNamed:#"download.jpeg"].CGImage;
[layer setNeedsDisplay];
or
remove [layer setNeedsDisplay];
I guess the setNeedsDisplay made the layer to redraw the content and so deleting the image.
--

Show Highlight on a UIButton with Subview

I'm trying to create a UIbutton that has the following characteristics:
* A custom image as its content
* Rounded corners
* A drop shadow
Using the following code will format the button to appear correctly and function but the highlight action (i.e., the darkening of the button when touched) is lost when the touch occurs. Any ideas?
+(void) styleButton:(UIButton*)button{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height)];
CALayer *sublayer = [CALayer layer];
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height);
backgroundView.userInteractionEnabled = NO;
[backgroundView.layer addSublayer:sublayer];
[button insertSubview:backgroundView atIndex:0];
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = sublayer.bounds;
imageLayer.cornerRadius = 10.0;
imageLayer.contents = (id) [UIImage imageNamed:#"myImage.png"].CGImage;
imageLayer.cornerRadius = 5.0f;
imageLayer.masksToBounds = YES;
[sublayer addSublayer:imageLayer];
}
Have you tried converting your backgroundView into a UIImage and add it to the button instead of adding backgroundView as a subview? That would preserve the darkening when the button is pressed:
UIGraphicsBeginImageContext(backgroundView.frame.size);
[backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setImage:image forState:UIControlStateNormal];
Like this (ARC enabled):
UIGraphicsBeginImageContextWithOptions(self.button.bounds.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIView *aView in self.button.subviews) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, aView.frame.origin.x, aView.frame.origin.y);
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
CGContextRestoreGState(context);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.button setImage:image forState:UIControlStateNormal];
[self.button.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
[self.button setAdjustsImageWhenHighlighted:YES];

Resources