What is wrong with the last line of code here? - ios

Am trying to set up a view hierarchy in the viewDidLoad method, and am getting a problem with the last line.
UIView *catView = [[UIView alloc] init];
UIImage *image = [UIImage imageNamed:#"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:imageView];

It looks like you want to change
[self.view addSubview:imageView];
to
[catView addSubview:imageView];
See https://teamtreehouse.com/forum/programming-a-background-stuck-on-code-challenge

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

How do I load image to a view?

I'm very new to ios. I'm trying to load a image to my view. Here is my code:
-(void)loadView
{
UIImageView *imageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
UIImage *image = [UIImage imageNamed:#"test.jpg"];
imageHolder.image = image;
[imageHolder sizeToFit];
[self.view addSubview:imageHolder];
}
in my class named Test which is a sub class of UIViewController. test.jpg is in the same directory. When I run, I'm getting an error at this line:
UIImage *image = [UIImage imageNamed:#"test.jpg"];
Thread1:EXC_BAD_ACCESS(code=2,address=0xbasd)
where I'm making mistake?
I'm using ios7 and xcode 5
I see the problem in your code. It cause infinity loop.
loadView - this method is called when the view is empty and you are responsible of creating setting view.
When you access self.view your view is empty and loadView method will be called again, and it will finish in infinite loop.
UIImageView *imageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
UIImage *image = [UIImage imageNamed:#"test.jpg"];
imageHolder.image = image;
[imageHolder sizeToFit];
self.view = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:imageHolder];
It's not important where image is located in you file structure. When you add a image to the project.
You can do it by - drag and gripping image to the project. or by - "Add Files to Project"
When you add image you should select a target project you are adding it to.
Instead of using a loadView method, I think you should put your code inside -(void)viewDidLoad method. By the way, in Xcode5, you need to right-click on your project and "Add files to project" to add this image file.
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
UIImage *image = [UIImage imageNamed:#"test.jpg"];
imageHolder.image = image;
[imageHolder sizeToFit];
[self.view addSubview:imageHolder];
}

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.

UIImageView not showing on iOS6

in ios5 i implemented my UIImageView like this:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"FirstScreen.png"]];
imageView.frame = CGRectMake(0.0,0.0,[GeneralUtils screenWidth],[GeneralUtils screenHeight]);
// !!!!!!!!!!!!!
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
[self.view sendSubviewToBack:self.tableView];
Everything´s fine...But now in ios6 my UIImageView is not showing anymore....
try bellow code..
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"FirstScreen.png"]];
imageView.frame = screenBounds;
[self.view addSubview:imageView];
[self.view bringSubviewToFront:self.tableView];
[self.tableView setBackgroundColor:[UIColor clearColor]];
Solved it by setting
[self.tableView setBackgroundView: nil];
I think my tableView was hiding the Image!

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

Resources