Add UIImageView as Subview - ios

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;

Related

adding UIImageview in titleview of UINavigationBar not working as expected

So i am trying to add an UIImageview in titleview property of the UINavigationbar using the following code. The issue is that although i put specific size to the UIView that contains the UIImageview which contains the image it does not work as expected. The result is a bigger image in the Navigation bar than the one i set with the CGRectMake. Also in some views it does not align to the center! What am i missing? Is there another way to add the image to the navbar?
-(void)viewDidLayoutSubviews{
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,10.0f,16.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,10.0f,16.0f)];
CGSize imageSize = CGSizeMake(10, 16);
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);
imageView.frame = CGRectMake(marginX, 11, imageSize.width, imageSize.height);
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage sdk_imageName:#"logo.png"];
[imageView setImage:image];
[containerView addSubview:imageView];
self.navigationItem.titleView=imageView;
}
I reviewed your code and i found some frame issues and at the bottom you are not passing container view in titleview, that will not render the view as expected.
Please try this one.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
UIImageView *imageView = [[UIImageView alloc] init];
//containerView.backgroundColor = [UIColor redColor];
CGSize imageSize = CGSizeMake(60, self.navigationController.navigationBar.frame.size.height);
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);
imageView.frame = CGRectMake(marginX, 0, imageSize.width, imageSize.height);
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed: #"2.jpg"];
[imageView setImage:image];
[containerView addSubview:imageView];
self.navigationItem.titleView=containerView;
Result:
Much simpler approach... use auto-layout constraints for the image view size... centering is handled automatically.
10x16 image view as the titleView:
UIImage *img = [UIImage imageNamed:#"logo"];
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:img];
titleImageView.contentMode = UIViewContentModeScaleAspectFit;
titleImageView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:#[
[titleImageView.widthAnchor constraintEqualToConstant:10.0],
[titleImageView.heightAnchor constraintEqualToConstant:16.0],
]];
self.navigationItem.titleView = titleImageView;

UIImage animatedImageNamed repeat count

I have a images sequence animated with :
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
[UIImage animatedImageNamed:#"spinner-" duration:1.0f];
imgView.contentMode = UIViewContentModeCenter;
imgView.animationRepeatCount = 1; // No effect
[self.view addSubView:imgView];
But the sequence keeps looping. Is there a way to control the repeat count (and keep displaying the last image) ?
In your case, using [UIImage animatedImageNamed:#"spinner-" duration:1.0f]; won't give you what you want.
For what you would like to achieve, I would recommend using UIImageView.
Usage:
First create an UIImageView object:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:<FRAME_SIZE>];
Second, create an array that holds your images to be animated.
If you just have a few, do the following:
NSMutableArray *animatedImagesArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"spinner-1.png"], [UIImage imageNamed:#"spinner-2.png"], nil];
If you have more, then you could load your images into the array like this:
for (int i = 0; i < <NUM_OF_IMAGE_COUNT>; i++)
{
[animatedImagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"spinner-%d%#", i, #".png"]]];
}
Once all that is done, then we configure the controls you want:
imageView.animationImages = animatedImagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 1.0f;
Then start animating it by:
[imageView startAnimating];
And stop the animation via:
[imageView stopAnimating];
If you would like the UIImageView to hold the last image after stopping, we would need to carry out the following before starting the animation:
imageView.image = [imageView.animationImages lastObject];
....
[imageView startAnimating];
Hope this helps.
You set the repeat count in the UIImageView object that displays your image.
UIImage *testImage = [UIImage animatedImageNamed:#"Box.jpg" duration:1.0f];
UIImageView *imgView = [[UIImageView alloc] initWithImage:testImage];
[imgView setAnimationRepeatCount:1];

How to display image in iphone simulator using objective C

I am trying to display image in iphone simulator programmatically using objective c, I am using following code, but it is not displaying anything in simulator
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
You can use this code
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
[self.view addSubview:imageview];
Maybe you forgot this:
imageView.frame = self.view.bounds;
[self.view addSubview:imageview];
If you are programmatically creating the imageView then you must add it to the view heirarchy using below line
[self.view addSubview:imageview];
Add your image view into view like this
imageview = [[UIImageView alloc] initWithframe:CGRectMake(0, 0, 100, 200)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[self.view addSubview:imageview];
First you need to initialize your UIImageView and assign UIImage to it's image property. You already did it in your code. Then you can set the frame to image view.
imageview.frame = CGRectMake(50, 50, 150, 150);
But in most cases it will be more useful to pass sizeToFit message to your image view.
[imageview sizeToFit];
So it will change it's frame to fit image's size. But using this approach you need to assign the origin to your image view's frame later. So the best solution is assign the frame in initialization and then pass sizeToFit.
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 20.0, 0.0, 0.0)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[imageview sizeToFit];
And don't forget to add image view to view hierarchy.
[self.view addSubview:imageview];
Also you need to check if your image is correctly added to your project. You can check it by finding you image file in Project Navigator and checking it's Target Membership in File Inspector. There should be a tick beside you target's name.

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

how do you center a custom UINavigation Bar Image

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

Resources