How to display an UIImage in a custom cell - ios

I am trying to display an Logo image in my custom cell, from amazon Amazon S3 bucket though StackMob
but its not showing. if l paste the direct url path to the image it works, how do i get around this.
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
CGRect imageFrame = CGRectMake(2, 2, 67, 67);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
NSURL* imageURL = [NSURL URLWithString:[object valueForKey:#"restoLogo"]];
NSData *data = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
self.customImage.image = tmpImage;
[cell.contentView addSubview:self.customImage];
the image is at this path [object valueForKey:#"restoLogo"] now returns the s3 url for the data.

If you are not using NSString's instance method
stringByAddingPercentEscapesUsingEncoding:
that could be the issue.
For example
NSString *escapedString = [imgURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Then turn it into a URL and continue on your way.

Related

Why is NSFileManager unable to find these png files?

I have written code to open image files after studying answers to the questions found here (a, b, c, d, e, f & g). But NSFileManager is unable to find them even though I added the png files to the project. I'm reasonably confident my code should be able to recognise either of the png files if they were in the right directory.
e.g.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSLog(#"\ndirPaths %# \n\ndocsDir \n%#", dirPaths, docsDir);
NSString *imageFilePath = [docsDir stringByAppendingPathComponent:#"iTunesArtwork1024.png"];
NSLog(#"\n\nfile path should be \n\n%# \n\n", imageFilePath);
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(#"\nFound file path : %#", imageFilePath);
}
else
{
NSLog(#"\nImage file not found");
}
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
[self.view addSubview:logo];
}
But here is the log in the debug window
2017-07-14 18:26:35.679 IconShape[1089:348564]
dirPaths (
"/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents"
)
docsDir
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents
2017-07-14 18:26:35.679 IconShape[1089:348564]
file path should be
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents/iTunesArtwork1024.png
2017-07-14 18:26:35.679 IconShape[1089:348564]
Image file not found
Here is the state of the project after two png files were added.
Yet the log shows they are not visible to NSFileManager. So where would they be found ? i.e. what changes do I need to make to my code in order to find them ?
EDIT
This draft finds the png file following Subramanian's recommendation.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *imageFilePath = [[NSBundle mainBundle]pathForResource:#"iTunesArtwork1024" ofType:#"png"];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(#"\nFound file path : %#", imageFilePath);
}
else
{
NSLog(#"\nImage file not found");
}
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
logo.image = image;
[self.view addSubview:logo];
}
The log now reads
Found file path : … .app/iTunesArtwork1024.png
and the image also appears in the subview.
__
Image is not in theDocument Directory, It's inside the bundle.
You have added the image files inside the project. But You are checking the image on Document Directory, Which is wrong. Image is inside app bundle.
You can simply assign the Image to UIImageView by the name of the image.
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
If you want to get the path of the image, then you have to use [NSBundle mainBundle]
[[NSBundle mainBundle]pathForResource:#"iTunesArtwork1024" ofType:#"png"];
You can access images with names which are added in projects. Try with below code
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
The path where your are looking for the image is inside your device (real or simulator).
To load an image that is stored in your XCode project just do:
UIImage* image = [UIImage imageNamed:#"iTunesArtwork1024.png"];

How to display external image to textview?

I have the following code to display internal image in textview. I am trying to display external http:// link to textview.
The following code works ok. Please help me to display external weblink image.
UITextField *txtstate =[[UITextField alloc]init]; [txtstate setFrame:CGRectMake(10, 30,170, 30)];
txtstate.delegate=self;
NSString * quotStr03 = [[NSString alloc] initWithFormat:#"%#",[dict valueForKey:#"deal-image"]];
txtstate.text=quotStr03;
txtstate.borderStyle = UITextBorderStyleLine;
txtstate.background = [UIImage imageNamed:quotStr03];
[txtstate setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:txtstate];
-(UIImage *) getImageFromURL:(NSURL*) imageURL
{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
by providing url to this function you will get external weblink image

ios load an image from docDir into UIimagview - viewDidLoad

I have a group of images saved in my DOcuments folder of the app. I got it to save to that location, I am having trouble loading from that location.
I got the UIimageView working hardcoded with images I dragged to a folder in the project with the code below in the viewDidLoad() method and when i ran the simulator it appeared.
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pants.png"]];
bottomPic.frame = CGRectMake(65, 279, 200, 250);
[self.view addSubview:bottomPic];
[bottomPic release];
However when I want to load the image from the Documents Directory nothing loads - Am confused and couldn't find any exact information to help me out on this situation.. This is currently what I have and when I run it doesn't throw any errors but no image appears. Am I missing something in my code? (I'm new to objective-c!)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"bottom1A2A6520-F556-436F-A1BC-D430223DC890-33098-00014708AE7E370F.png"]];
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:path]];
bottomPic.frame = CGRectMake(65, 279, 200, 250);
[bottomPic setImage:image];
[self.view addSubview:bottomPic];
[bottomPic release];
A path is a path and an image is an image.
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:image];
Your code has a few issues. Try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"bottom1A2A6520-F556-436F-A1BC-D430223DC890-33098-00014708AE7E370F.png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:image];
bottomPic.frame = CGRectMake(65, 279, 200, 250);
[self.view addSubview:bottomPic];
[bottomPic release];
Unrelated to the problem, you were needlessly using stringWithFormat:.
The real problem was creating the UIImageView with an unneeded image using imageNamed:.
If this doesn't work then verify that path actually represents a file that really exists in the Documents folder. Verify image isn't nil.
Also consider use ARC (automatic reference counting) to make your life a lot easier.
Update: It appears you needed NSDocumentDirectory instead of NSDocumentationDirectory.

UIImage with NSData initWithData is nil

The following code does not seem to load an image.
uiTabBarItem = [[UITabBarItem alloc] init];
NSData *datatmp = [NSData dataWithContentsOfFile:#"newsicon.png"];
UIImage *tmp = [[UIImage alloc] initWithData:datatmp];
uiTabBarItem.image = tmp;
datatmp is nil (0x000000) and
the image does exist.
I. Don't reinwent the wheel. Use tmp = [UIImage imageNamed:#"newsicon.png"]; instead.
II. NSData expects a full file path when being initialized from a file. The following would work (but you don't have to use this anyway, as I just pointed it out):
NSString *iconPath = [[NSBundle mainBundle] pathForResource:#"newsicon" ofType:#"png"];
NSData *datatmp = [NSData dataWithContentsOfFile:iconPath];
Loading an image from a file is best accomplished with:
[UIImage imageNamed: "newsicon.png"];

xcode get black window using storyboard

I'm trying to get a view but, when I get to it in iPhone simulator what I see is a black window and I don't now what I'm doing wrong.
Here's my code:
subview = [[ArticulosViewController alloc]init];
subview.titulo = categoria;
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat: #"http://XXX.XXX.X.XX/XX/%#",[[self.listaArticulos objectAtIndex: [indexPath row]] imagen]]]];
subview.imagen = [UIImage imageWithData: imageData];
[self.navigationController pushViewController:subview animated:YES];
In ArticulosViewControllew I don't have the method -(void)LoadView{}, so I'm quite lost in what I've to do
Thank you all for your help

Resources