I want to show a UIImage with following code:
UIImageView *image = [[UIImageView alloc ]initWithFrame:CGRectMake(0, 0, 500, 500)];
image.image = [UIImage imageNamed:#"1.png"];
[self.view addSubview:image];
And here is my Xcode config screenshot, just drag and drop the gifs folder into the Xcode with copy if necessary
try this.
image.image = [UIImage imageNamed:#"gifs/1.png"];
How to add image folders
Related
I have an UIImage that I assign to an UIImageView.
When I do :
UIImage *picto = [UIImage imageNamed:#"animage.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:picto];
it works, but when I do :
UIImage *picto = [UIImage imageNamed:#"animage2.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:picto];
My image 2 isn't displayed, the grammar is correct and my image exist in the project...
By the way, the second image is not displayed just because it name
use this format :
UIImageView *icon_bg=[[UIImageView alloc]init];
icon_bg.image=[UIImage imageNamed:#"animage.png"];
[Cell.contentView addSubview:icon_bg];
In our iOS app, we want a loading image which is just our logo, animated. I'm wondering what the best practice is to do this? Would it be to use a series of .png images, or one long .png image. I can also thing of using a .gif but the colour quality is not the greatest.
It may also be possible, since the animation is just resizing of certain elements, to do the animation programatically using several UIimages and resizing them, though it may not be as smooth.
UIImage* img1 = [UIImage imageNamed:#"image_1.png"];
UIImage* img2 = [UIImage imageNamed:#"image_2.png"];
UIImage* img3 = [UIImage imageNamed:#"image_3.png"];
UIImage* img4 = [UIImage imageNamed:#"image_4.png"];
UIImage* img5 = [UIImage imageNamed:#"image_5.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4,img5,nil];
UIImageView* imageView = [[UIImageView alloc]
initWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 50, 80.0, 80.0)];
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:100];
[imageView setAnimationDuration:3.0];
[imageView startAnimating];
imageView.tag = 100;
[self.view addSubview:imageView];
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];
}
hai iam new one for objective c and iOS development
Set the UIImageview image. try the following code
arrowImgVw is UIImageView Class object
UIImage *image=[[UIImage alloc]init];
image=[UIImage imageNamed:#"return_journey_denoter.png"];
arrowImgVw.image = image;
hey you are wrong in some point use this code for adding an image into UIImageView and display it on iPhone devide
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
//Allocating imageView and set its frame.
myImage.image = [UIImage imageNamed:#"return_journey_denoter.png"];
//add image into imageview
[self.view addSubview:myImage];
// add imageview into your viewcontroller's view
To simply set image to imageView just one line of code
arrowImgVw.image = [UIImage imageNamed:#"return_journey_denoter.png"];
The code you have written is ok for setting image, just check the name for the given image or you can try following code...
UIIImageView *arrowImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,200,200)]; //just example change the frame as per your need
arrowImgVw.image = [UIImage imageNamed:#"return_journey_denoter"];
If you are trying to access the image from the assets . Use the below code
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
UIImage *image = [UIImage imageNamed:#"image" inBundle:bundle compatibleWithTraitCollection:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
image should be inside your assets folder .
In Swift3.0
To set image to your imageView use belwo code.
arrowImgVw.image = UIImage(named: "return_journey_denoter.png")
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.