How to display image in iphone simulator using objective C - ios

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.

Related

Bug about UIImageView display gif?

I create a UIImageView and add to VC UIView , then I create a image with animatedImageWithImages:duration:
When I execute the code, it works normally. It displays image animation. But when I push to next VC and in the process of swipe back , I find the UIImageView display nothing.
At the moment finger left screen , everything back to normal.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
[self.view addSubview:imgView];
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}
BugGif
I notice the imageView has CAKeyframeAnimation. I guess the conflict that runmode and CAKeyframeAnimation.
SDWebImage has the same problem. Because it also use the method that create UIImage. I think the point is the method of animatedImageWithImages:duration: and CAKeyframeAnimation.
Anyone knows the solution? Thanks a lot.
The event that the finger left the screen will make imageView display normally. Why and How to resolve?
GitHubBugDemo
You can use UIImageView another methods to set images and animation duration as below
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];

Logo in Nav Bar Obj-C

I am trying to display my logo in my Navigation Bar but I can't get it to fit all the different sizes of phones. I can get it to fit one and it looks terrible on another. This is the closest Ive gotten to it looking good at all. Does anyone see how I could make this look better?
UIImage *logo = [UIImage imageNamed:#"WTB_LOGO"];
UIView *headerView = [[UIView alloc] init];
headerView.frame = CGRectMake(0, 0, 225, 115);
UIImageView *imgView = [[UIImageView alloc] initWithImage:logo];
imgView.frame = CGRectMake(0, 0, 205, 115);
imgView.contentMode = UIViewContentModeScaleAspectFit;
[headerView addSubview:imgView];
self.navigationItem.titleView = headerView;
If I understood the problem your image is not centred properly within headerView. If image is all you want just don't use header at all, titleView is centred automatically.
self.navigationItem.titleView = imgView;
If you have a back button it will push the image View. Here's how it looks:
So you have to make your image a little smaller.
Not sure why just not simply try this:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
Or I'm missing anything?
Have you tried setting it like:
...
UIView *headerView = [[UIView alloc] init];
headerView.frame = CGRectMake(0, 0, 225, 115);
...
// same frame with the headerview
imgView.frame = headerView.frame;
// Note: should be `imgView.frame = headerView.frame;`
// `imgView.frame = CGRectMake(0, 0, 225, 115);` is different
//
// this also work if you are supporting multi-orientation
//
// or simply assign the imageView directly
self.navigationItem.titleView = imgView;
// but the width is kinda broad `225` it will probably be push if you insert UIBarButtonItem to left/right
Since you are using UIViewContentModeScaleAspectFit it will automatically scale itself to fit the frame.
if ever you needed to rescale the image here is a code for it.
+ (UIImage *)imageWithImage:(UIImage *)image scaleToSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Hope this helped you. Cheers! :)

How to set the image in Background image in UIViewController in ios

How to set the image in Background image(means how to paste the image in Background image). I took one image as a background image in UIViewController. Right now i want to put the another image in background image.i know how to put the image as a Background image in UIViewController.But i have no idea how to put the image in background image.I tried the code.But the image will not displayed in background image.This is my code.please help me any body.Thanks in advance.
-(void)viewDidLoad
{
backgroundImage=[UIImage imageNamed:#"bg-small.PNG"];
audioViewBackgroundImage=[[UIImageView alloc]initWithImage:backgroundImage];
[self.view addSubview:audioViewBackgroundImage];
mImage=[UIImage imageNamed:#"muluguphoto.png"];
mBackgroundImage=[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage=[[UIImageView alloc]initWithImage:mImage];
[audioViewBackgroundImage addSubview:mBackgroundImage];
}
add it to your viewDidLoad..
UIImageView *newImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newImage setBackgroundColor:[UIColor redColor]];//here else you can add image
[self.view addSubview:newImage];
UITableView *newTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newTable setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:newTable];
you have to set imageview above imageview in userinterface.like in photo.make proper heirARCHY!and set frame of imageview INDependentaly as you want.here is background image with logo image..
Add this line to viewDidLoad
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Why do you initializing mBackgroundImage two times?
Let's try the below code and let me know the result.
UIImageView * audioViewBackgroundImage=[[UIImageView alloc] initWithFrame:self.view.frame];
audioViewBackgroundImage.image = [UIImage imageNamed:#"bg-small.PNG"];
[self.view addSubview:audioViewBackgroundImage];
UIImageView * mBackgroundImage =[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage.image = [UIImage imageNamed:#"muluguphoto.png"];
[audioViewBackgroundImage addSubview:mBackgroundImage];
Thanks!
UIImageView * bgImage =[[UIImageView alloc]initWithFrame:self.view.frame];
bgImage.image = [UIImage imageNamed:#"myimg.png"];
[self.view addSubview:bgImage];
[self.view sendSubviewToBack:bgImage];
//write this line in viewdidload and make sure that you have import the image in your project by copying it .
when you write this line of code then this image will automatically replace the previous background image
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"stack.jpeg"]];

How to change the image within a UIImageView inside an iOS app?

I have a UIImageView object that I instantiate and turn into a property inside my iOS app. I initialize the attributes for this UIImageView object inside my viewDidLoad method, however, I need to change the image that is contained inside the UIImageView object later on in another method. When I do this, I for some reason am simply superimposing the new image over top of the old one. What I would like to do is simply resize the frame, and replace the old image with the new one.
Here is the relevant code:
Inside my viewDidLoad method:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 250, 300, 200)];
imageView.image = [UIImage imageNamed:#"firstImage.png"];
[self.view addSubview:imageView];
and inside my method where I wish to make the changes:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 250, 200, 300)];
imageView.image = [UIImage imageNamed:#"secondImage.png"];
[self.view addSubview:imageView];
Please bear in mind that I don't want to just change the image, but also the frame size to accommodate the new image. Can anyone see what I am doing wrong?
You are creating another instance of imageview. You need to access the same object and change frame and image there.
Try this,
Create imageView as property in the class as,
In .h file,
#property(nonatomic, retain) UIImageView *imageView;
Then in viewDidLoad,
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 250, 300, 200)];
self.imageView.image = [UIImage imageNamed:#"firstImage.png"];
[self.view addSubview:imageView];
The in second method, just change the frame and image as shown below,
self.imageView.frame = CGRectMake(10, 250, 200, 300);
self.imageView.image = [UIImage imageNamed:#"secondImage.png"];
As per you code, you were creating separate instances for imageViews and adding as subviews which will cause it to superimpose on another one. Instead you need to create a single instance as shown above and modify its frame and image properties whenever required.

UIImageView shows outside its parents frame

I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;

Resources