Can not load UIImage on button - ios

I want to add UIImage on button,but the del.png can't show on my button,
What can i do?
I try many load Image Method,but no effect,such as:
UIImage* delImage = [[UIImage alloc] initWithContentsOfFile:#"del.png"];
UIImage* delImage = [UIImage imageNamed:#"del.png"];
UIImage* delImage =[UIImage imageWithContentsOfFile:#"del.png"];
CalDelBtn *BookItem = [[CalDelBtn alloc] initWithFrame:CGRectMake(startPointX, startPointY, 75, 113) statuePicture:delImage];
Please give me some advice,thank you!!

First, you image could be loaded with
UIImage * image = [UIImage imageNamed:#"del.png"];
This is the simplest way to load an image, so it should work with an autoreleased image.
Don't forget to add it to your xCode projet explorer! Here's a list of reasons for what your image generally woudn't display : https://stackoverflow.com/a/9698764/127493.
Then, add it to your button with :
[button setBackgroundImage:image forState:UIControlStateNormal];
for example. Does it work ?

Can you verify that your image is being added to your target? Xcode 4.3 has a nasty habit of not including files into targets when you drag and drop. So click on your project file on the left side of Xcode, go to Build Phases and see if your image is in the list under Copy Bundle Resources.

Related

Since the last Xcode update I can't work with images

This is the basic code I use to add an image named "Background.png" to an UIImageView:
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:#"Background.png"];
imageView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:imageView];
Since the last Xcode update the resource can't be loaded.
I tried running on an iPhone 6 - iOS 10.3 and the Simulator - iOS 11.0
For some reason even though the iOS deployment target is version 10.3 the simulator runs on iOS 11.0
The image is there, in the same folder as the project named: "Background.png"
I've been doing the same thing for years and now it has stoped working.
Anyone had a similar issue?
You may want to utilize the purpose of Assets.xcassets. Put your assets there and you can easily use your images. Note that you do not have to put the image's file extension. It's easy, like so:
UIImage *markerImage = [UIImage imageNamed:#"marker"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:markerImage];
[imageView setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:imageView];
I have an image in my Assets.xcassets named marker.
Edit: this is tested, I made an Objective-c project just for this question :)
I had the same problem and i solved it as follow:
Simply Select your image, then check the box linked to your projet name in "Target Membership" (on the Right Window)
is it working ?
There is a Screenshot if needed:
https://i.stack.imgur.com/gTOGJ.png

Repeat image horizontally and vertically

I want to repeat the following image:
to achieve the following background:
I tried a few codes as follow:
bluePatternView.backgroundColor = [UIColor colorWithPatternImage:
[[UIImage imageNamed:#"blue_pattern.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]];
and:
bluePatternView.backgroundColor =
[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"blue_pattern.png"]];
I also tried to draw image with CGContextDrawTiledImage with no success.
How that can be done?!
EDIT: result of implementing luk2302. please give him recognition by upvoting
bluePatternView.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_pattern.png"]];
result:
I found solution in images.xcassets,
Step 1:- Put your image to images.xcassets
Step 2:- Click on image, then click on bottom right corner written “Show Slicing”
Step 3:- Click on Start Slicing
Step 4:- Click on "Slice Horizontally and Vertically" button
Step 5:- Here you will see 3-Horizontal and 3-Vertical slice lines.
Put the most left vertical line to left side of image and both right most lines to right side of image.
Put the most top line to top of image and both bottom most lines to bottom of image.
So the final result will be looking like this.
Step 6:- Use this image.
And now image will be repeated.
Note:- If you give slicing to 2x image it will repeat just 2x image, for 3x images you need to do the same slicing.
Example by Apple
As my comment already said: use
bluePatternView.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_pattern.png"]];
You dont want to stretch your image. Let UIKit take care of the repetition.
Assuming that your bluePatternView is the actual large view for which you want to set a pattern as background. Maybe you are setting the background property of the wrong view if that code is still not working.
Note: duplicating the SAME image with #2x and #3x extension will lead to behaive properly for better resolution devices.
bluePatternView.backgroundColor = UIColor(patternImage: UIImage(named: "blue_pattern")!)
You need to save image in Assets then put those lines in viewdidload
bluePatternView.backgroundColor = UIColor.init(patternImage: #imageLiteral(resourceName: "f8"))
This is for Swift 4.1.
this with Bellow Method using stretchableImageWithLeftCapWidth like this way:-
UIImage *backgroundImage = [[UIImage imageNamed:#"SheetBackground.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
as par your need example:-
UIImage *backgroundImage = [[UIImage imageNamed:#"q4Ses.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
[_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];

How can parse all the images form Website in IOS SDK

I like to implement image upload functionality in my app.For that I like to give some option for user to choose image.
One of the option is choose image from website.
If the user enter an url link in the text box and click the get images button means. I like to display all the images in that particular link
But I am not having any idea about this.
I have explain my requirement below
1)Enter the url in a text box
2)Click the get images button
3)Parse all the images from that particular url
4)Display all the images in the Imageview
And also to illustrate my requirement I have attached the screenshot below
If anybody work around this functionality means please suggest me some idea
Thanks
Like this ?
- (IBAction)buttonClicked:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.yourwebvsite.com/image.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData: data];
UIImageView *displayImageView = [[UIImageView alloc] initWithImage:image];
[displayImageView setFrame:CGRectMake(0, 0, 300, 300)];
[displayImageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:displayImageView];
}
Or if you want all the images from a web directory. See here and save all the images in an array then display the images in a UIImageView like my code above.

iOS: tab item image not loading

I want to change the image of my tab bar item using self.tabBarItem.image = [UIImage imageNamed:#"home_icon.png"]; Seems simple enough, but I can't get it to run. Note: I'm simply replacing the file name from first to home_icon. The code works with the name "first".
Details:
I have started with the standard tabbed application and have only changed this one line of code. Xcode then loads the 64x64 image. I placed a home_icon.png file in the project and made sure it's a 30x30 png file. The docs says: "This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object." Why can't I simple reference a different file here? I don't understand what is actually getting referenced here. The original line is self.tabBarItem.image = [UIImage imageNamed:#"first"];, to reference first.png.
What I tried (see answers so far):
using home_icon, instead of home_icon.png
Using UIImage *img = [UIImage imageNamed:#"home.png"];
self.tabBarItem.image = img;
Adding the 2x file
Checking output console. No errors mentioned. (The app still references the old file successfully).
Checking target. File is added to target
Remove the ".png" from the end.
you should try below code;
UIImage *img = [UIImage imageNamed:#"image.png"];
[[tabBarController.viewControllers objectAtIndex:tabImageIndex] tabBarItem].image=img;
Use the Following Code For Changing The Background Of Tabbar. You can Add different different images According to your Tab.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControllers{
NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewControllers];
switch (index) {
case 0:
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_act21.png"]];
break;
case 1:
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_act22.png"]];
break;
case 2:
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_act23.png"]];
break;
default:
break;
}
return YES;
}
if you want to Change only Tabbar item Image Then Use following Code.
self.tabbarcontroller.tabBarItem.image=[UIImage imageNamed:#"Someimage.png";
Are you sure that [UIImage imageNamed:#"home.png"] return not nil? Check in console
NSLog(#"%#", [UIImage imageNamed:#"home.png"]);
If so, try to add the image to the Build Phases -> Copy Bundle Resources
The solution was to use only alpha channels in the png file. See http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html for details.

How to retrieve image locally from application and display it on UIImageView with Objective-C

I have loaded the image in application's folder that is source
but I could not find the code by which I will display that image
on UIImageView using button click event. Please help me out for this.
I have used this following code before:
UIImage *img = [UIImage imageNamed:#"xyz.png"]
but I could not set the UIImageView image to img in the method
Then please help me with code to set the UIImageView image to img in the method I call on my button click.
You can get an image added to your project folder/subfolder using
UIImage *img = [UIImage imageNamed:#"xyz.png"]
Then set the UIImageView image to img in the method you call on your button click.
Drag the image into Xcode and then set up an UIImage variable like this:
UIImage *theImage = [UIImage imageNamed:#"theImage.png"];
then implement it into your UIImage view by 'myImageView.image = theImage;
Make sure you have IBOutlet set up to your image view, though. The easiest way is to just drag the image into Xcode and select it under the popup list in Interface Builder.

Resources