Here's my code so far (in the draw rect):
// Drawing code here.
NSLog(#"%#", [[NSBundle mainBundle] pathForResource:#"NoiseBGMainView" ofType:#"jpg"]);
NSURL *pathToBGImage = [[NSURL alloc] initWithString:[[NSBundle mainBundle] pathForResource:#"NoiseBGMainView" ofType:#"jpg"]];
NSImage *NoiseBGMainView = [[NSImage alloc] initWithContentsOfURL:pathToBGImage];
[NoiseBGMainView drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
And the image doesn't draw... I don't know what's wrong.
Why not just use [NSImage imageNamed:#"NoiseBGMainView.jpg"] instead? That will give you an NSImage of your picture (if it can find it), and nil if it can't. NSLog what the results of that call are, then come and report back on whether it's logging (null) or not.
You can draw an NSImage into an NSView without writing any drawing code by attaching an NSImageView to your view and setting your image resource as the NSImageView's image. (NSImageView handles all the drawing).
If your custom NSView is loaded from a nib, you don't need any setup code either - just make the appropriate NSView-NSImageView-NSImage (resource) connections with Interface Builder.
NSURL *pathToBGImage = [[NSURL alloc] initWithString:[[NSBundle mainBundle] pathForResource:#"NoiseBGMainView" ofType:#"jpg"]];
This is wrong. -[NSURL initWithString:] takes a string version of a URL, such as http://apple.com/, not a pathname.
Because you didn't pass a URL, the URL will be a relative URL, but a relative URL needs a base. The URL is a valid object, but doesn't lead anywhere. This, in turn, causes initWithContentsOfURL: to return nil (because you tried to load an image from a relative URL with no base). You then try to tell the image to draw, but you don't have an image, so nothing happens.
You want initFileURLWithPath:. (Or the class method fileURLWithPath:, with which you won't have to release the URL later.)
Related
I have a very weird issue. I have a watch app and I am trying to display an image programatically. When I debug the code, I can see the image is correctly loaded but on simulator or real device I cannot see the image. I have added that image in both watch and extension bundle resources.
Firstly I use the code below to open the view.
NSArray *objects =[NSArray arrayWithObjects:#"MainPageView",#"InterfaceController",#"PoseImageView",nil];
[self presentControllerWithNames:objects contexts:nil];
Inside PoseImageView I am trying to display an image with:
UIImage *imgTest = [UIImage imageNamed:[NSString stringWithFormat:#"1.jpg", strAngle]]; // I can see 1.jpg in debugger
[_imgPose setImage: imgTest];
The image is not there on watch or simulator. I am wondering how to fix the issue.
Note: From assets its not loading as well.
Note2: I can only display the image if the view is initial view.
You are trying to use imageNamed for .jpg which will not work.
Try using
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"imageName"] ofType:#"jpg"];
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];
(This looks like a question that has been asked/answered 100 times but I can't get it to work.)
I have a view controller that is story board based. Nested in the main view is a webUIView.
In the controller method viewDidLoad I am simply specifying the URL and setting the view and the webUIView to autoresize:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"web_app_build"]];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_browser loadRequest:request];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = TRUE;
self.browser.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
I also added this method
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.browser.frame = self.view.bounds;
}
When I rotate the device the webUIView rotates, but doesn't resize to fill the screen. I did try writing my own rotation handler but this seems the entirely wrong way to do that.
I did read the apple doc on the topic. How can I make the webUIview resize to fit the display size on rotate?
The issue you're having is that self.view.bounds has most likely not been adjusted yet to the bounds after rotation. Set a breakpoint to check this out. I would move the code to viewDidLayoutSubviews: instead. If you don't need to support versions of iOS before iOS 6, I'd strongly recommend looking into using Auto Layout for your view objects. It will make your life a lot easier.
Currently, I'm working on the client for the website. I have tonne of images that I need to load to my TableView. Here is what I'm currectly doing:
NSDictionary *propertyItems = [self.items objectAtIndex:indexPath.row];
cell.fio.font = [UIFont boldSystemFontOfSize:17.0f];
if([propertyItems objectForKey:#"online"] == [NSNumber numberWithInt:1])
cell.fio.textColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.3 alpha:1.0];
dispatch_queue_t downloadPhotosQueue = dispatch_queue_create("downloadPhotosQeue", NULL);
dispatch_async(downloadPhotosQueue, ^{
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.pic.image = [UIImage imageWithData:photosData];
});
});
cell.fio.text = [propertyItems objectForKey:#"fio"];
I'm doing this in cellForRowAtIndexPath: method.
Everything is loading fast. But the problem is, when I'm scrolling my table down, and after again up, the images are reloading over and over again.
Question: Is there any way to easily cache my UIImages that I'm getting from the server? So if they are loaded once, they wouldn't reload over and over again, while I'm running the app. Maybe I'm doing something wrong?
Thanks in advance!
I strongly recommend AsyncImageView. I use it and it works like a charm. Just set the image url and it handles everything itself.
AsyncImageView *imageView = [[AsyncImageView alloc]init];
imageView.imageURL = [NSURL URLWithString:#"https://www.google.es/logos/classicplus.png"];
It caches the image in memory so it won't retrieve it from the server again. It will also release them when receiving a memory warning.
To load image from a website I use AFNetworking. It provides a class to load an image from an URL:
Start by adding #import "UIImageView+AFNetworking" to the top of a
controller or a view implementation file. This category adds methods
to UIImageView, like:
[imageView setImageWithURL:[NSURL URLWithString:#"…"]];
i recommend this library to accomplish what you want.
in cellForRowAtIndexPath:
[cell.imageView setImageWithURL:
[NSURL URLWithString:[NSString stringWithString:#"www.yourimagepath.path"]]
placeholderImage:[UIImage imageNamed:#"no_image.jpg"]];
the library will cache the image for you. you can also set the setHolder of the image, so it wont look like a blank image while the images are downloading.
The problem is here :
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dataWithContentsOfURL
It launch a request to the Url everytime the function is called (everytime you scrolled).
You need to load all the picture before cellForRowAtIndexPath.
Like in ViewDidLoad.
You can just store them in an array and display your picture's array in cellForRowAtIndexPath.
If it's really fast to load like you say : jsut load picture Once in CellForRowAtIndexPath. Store them in a mutableArray. And check if the picture already exist .
I have an NSBox set up in my main view that accepts drag and drop. We store the URL into str. We then load the image and add it to the content view of NSBox.
imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 390, 150)];
NSURL *imageURL = [NSURL URLWithString:str];
NSImage *image = [[NSImage alloc] initByReferencingURL:imageURL];
[imageView setImage:image];
[self setContentView:imageView];
However, this doesn't do anything. The image is not displayed in the nsbox.
Another oddity. At first I was trying to add the NSImageView via interface builder, but the only image container they had was IKImageView... Is this odd? Isn't NSImageView more ubiquitous? I mainly develop on iOS, so I have the version from the ios developer site.
Any thoughts?
Edit: I should also add, when I was using IKImageView in IB, the image would show up.
Edit2: I've also tried just taking the initWithFrame out and replacing it with just init, no go.
The problem was that I was using initByReferencingURL to get the NSImage. This doesn't work for local files, so instead I used initByReferencingFile and everything worked out well!
I know this seems like a simple task, which is why I don't understand why I can't get the image to render.
When I set up my UIView, I do the following:
myUiView.backgroundColor = [UIColor clearColor];
myUiView.opaque = NO;
I create and retain the UIImage in the init function of my UIView:
image = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"png"]] retain];
then my drawRect looks like this:
- (void) drawRect:(CGRect) rect
{
[image drawInRect:self.bounds];
}
Ultimately I'll be manipulating that UIImage via bitmap context, and then in drawRect create a CGImage out of the context, and render that, but for now I'm just trying to get it rendering a known image.
I've been digging through this site, as well as the documentation. I've gone down the CG path and tried drawing it with CGContextDrawImage by following the numerous examples other people have posted, but that didn't work either.
So I've come back to what seems to be the most straightforward way to draw an image, but it isn't working.
Any help would be greatly appreciated.
Thanks in advance.
First of all, verify that the size and position of self.bounds are what you want them to be. If the size is {0,0} nothing will display. Check using this function:
NSLog(#"%#", NSStringFromCGRect(self.bounds));
Also make sure that the image is not nil:
NSLog(#"%#", image);