So I'm trying to load an image from camera into image view. I've got it done, but images aspect ratio changes.
I tried setting content mode for imageview but no effect.
Here is how i do it:
UIImage *picture=[UIImage imageWithData:slika];
imageview = UIViewContentModeScaleAspectFit;
[imageview setImage:picture forState:UIControlStateNormal];
Any ideas?
Is image view your own view? Or it's, for example, cell's default imageView?
And you should write so:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:picture];
or
[imageView setImage:picture];
There is no method [imageview setImage:picture forState:UIControlStateNormal]; for UIImageView, it only available for UIButton (and may be for some other UIControls, but UIImageView not subclass of UIControl - it direct subclass of UIView) . But button does not scale images preserving aspect ratio.
Related
I am creating a tvOS app and I want to use parallax images on a couple of buttons. From the docs:
To incorporate parallax images in your app:
Create a UIImage object.
You load the image differently depending on whether the image is included in your app bundle or whether you have downloaded the
image.
Bundle—Load images using imageNamed:.
Downloaded file—Load images using imageWithContentsOfFile:.
Create a new UIImageView object using the loaded images.
If the UIImageView is part of another view, set adjustsImageWhenAncestorFocused to YES on the UIImageView.
I know it says UIImageView there, but I was hoping to make the same effect happen on a UIButton, like the home screen app icons.
I've created the artwork, made a stack in the asset catalog, and loaded the image with imageNamed:, but the UIButton does not behave like a parallax image. It does not sway around like the home-screen icons do. It just looks like a flat image.
Is there something else I have to enable in order for the UIButton to behave like the home screen app icons?
UIButton* quitGame = [[UIButton alloc] initWithFrame:rectWithNewX(playAgain.frame, 985)];
[quitGame setImage:[UIImage imageNamed:#"quit.lsr"] forState:UIControlStateNormal];
[quitGame setAdjustsImageWhenHighlighted:YES];
fadeIn(quitGame, self.view, 0.5);
As of right now, this is not possible with just UIButtons, however, I did find a workaround.
Instead, create a UIImageView that will fill the size of the UIButton and make it a subview of the UIButton. Then call the adjustsImageWhenAncestorFocused: and set it to true. Voila!
UIButton* playAgain = [[UIButton alloc] initWithFrame:CGRectMake(centerX(650, self.view), sh() - 250, 300, 180)];
[playAgain setAdjustsImageWhenHighlighted:YES];
[playAgain addTarget:self action:#selector(playAgain) forControlEvents:UIControlEventPrimaryActionTriggered];
fadeIn(playAgain, self.view, 0.5);
UIImageView* playAgainIGV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 180)];
[playAgainIGV setImage:[UIImage imageNamed:#"playagain.lsr"]];
[playAgainIGV setAdjustsImageWhenAncestorFocused:YES];
[playAgain addSubview:playAgainIGV];
I've had issues when setting files or precompiled LSRs.
Creating the LSR in XCode Asset Catalogue through Asset Catalogue -> New Apple TV Image Stack and dragging in the PNGs, then setting the image either through Interface Builder or through this works:
-(void) setLsr:(UIButton *)button lsrNamed:(NSString *)lsr {
[button setAdjustsImageWhenHighlighted:YES];
UIImageView *biv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, button.frame.size.width, button.frame.size.height)];
[biv setImage:[UIImage imageNamed:lsr]];
[biv setAdjustsImageWhenAncestorFocused:YES];
[button addSubview:biv];
}
Regarding the answer of david I made a simple Swift method to create a parallax effect button:
func createParallaxButton(button: UIButton, imageNamed: String) {
button.adjustsImageWhenHighlighted = true
let buttonBg = UIImageView(image: UIImage(named: imageNamed))
buttonBg.adjustsImageWhenAncestorFocused = true
buttonBg.frame = button.bounds
button.addSubview(buttonBg)
}
createParallaxButton(myButton, imageNamed: "myButtonimage.lsr")
Through Face Detection, I want to blur eyes and mouth of a person. So I have a imageView that contains 3 subviews (2 per eye and the mouth). Each one of these subviews were masked with a PNG shape (with background clear) for avoiding to show rectangle.
My imageView in screen remain so: http://screencast.com/t/ak4SkNXM0I
And I want to obtain the image for storing in another place, so I've tried this:
CGSize size = [imageView bounds].size;
UIGraphicsBeginImageContext(size);
[[imageView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
But finalImage is an image like this:
http://screencast.com/t/eDlvGqqY
My subViews (eyes and mouth) are not masked as above.
Any idea?
Thanks.
Edit:
I have to use library compatible with ios6
You can check the new API added to iOS7. Try one of the following methods:
snapshotViewAfterScreenUpdates:
resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets: for resizable image
drawViewHierarchyInRect:afterScreenUpdates:
I have a couple of arcs and rects in a UIView, and a UIImageView as well. I want to have the UIImageView in the background. I have ordered it in IB so that the image view is behind my labels and buttons.
First, I need to put an image in the image view, which I don't want to do in IB. I've tried this code
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:#"myImage"];
But it doesn't work. After I get that working, what can I do to move my paths in front of the image view?
I think you want to draw to put a background image and on top of it want to draw circles(arcs).
If my understanding is correct. First draw the image on to the view using CGContextDrawImage() and then draw arcs on top of it. (No need to use image view).
Add image to UIImageView :
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:myImageView];
I've got an app that has two different background images. The one selected is determined by the orientation. When I start out, I check self.interfaceOrientation, and then go and pick the proper image. However, whenever the view opens, part of the image repeats instead of stretching. I saw a previous answer applying autoresizing masks to an imageview, but there is no imageview that I'm currently using.
Inside the loadView method:
if(self.interfaceOrientation==UIInterfaceOrientationPortrait ||self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"portrait"]]];
}else{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"landscape"]]];
}
As rishi had pointed out, the issue was caused by the colorWithPatternImage method. What I did to resolve this was to set the background of the view to be a specified image.
UIImageView* bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed: #"foo.png"]];
[self.view addSubview: bgView];
I also added in flexible width and height so that it would rotate properly.
I am trying to adjust the height of an image by stretching it.
This is what I've got so far:
-(IBAction)buttonTapped{
UIImage *img = [UIImage imageNamed:#"water.png"];
[water1 setImage:img];
}
-(IBAction)button2Tapped
+ (UIImage*)imageWithImage:(UIImage*)img
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[img drawInRect:CGRectMake(0,2,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;}
and this is in my view controller.h
IBOutlet UIImageView *water1;
-(IBAction)buttonTapped;
-(IBAction)button2Tapped;
I am not sure what I need to change to make this correct, or if I should start again with a new way.
I want it so that when I push a button, an image appears, then it resizes it when I push another button.
Thanks for helping!
You can simply change the size of the UIImageView property, in order to get the size you want.
Set the contentMode of the UIImageView to wathever fits your need, and it will automatically resize to the given size (either scaling to fit or stretching).