Adding code to a storyboard's UIImageview - objective-c

I'm trying to get an image from an URL and display it within a UIImageview.
However, I can't connect my code to my image because I don't know how to.
Previously I just made a nib file and do the coding there, but since I transfered my App to iOS 6 and would like to use a storyboard I don't know how to achieve the same thing:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString * imageURL = [NSString stringWithFormat:#"http://app-content.app.com/api_ios/images/%#.png",
[[NSUserDefaults standardUserDefaults] objectForKey:#"setup"]];
NSURL* url = [NSURL URLWithString: imageURL];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
}
As you can see, I'm loading an image from a website and change a string from the URL with the string from the NSUserdefaults.
I would like to link imageview to the image in my storyboard but it won't let me, nor can I use an identifier on the UIimageview.
What is the right way to do this with a storyboard?

Pedros was right, the IBoutlet was not connected the right way.
It is working now.

Related

Change UIImage imageOrientation metadata without affecting the image

I have an app which loads an image from the local disk and then displays it on a UIImageView. I want to set the image to aspect scale to fit.
The problem I'm having is that the imageOrientation is coming back as UIImageOrientationRight even though it's a portrait image and that's messing with how the aspect calculations are done.
I've tried a few methods of changing the meta data but both rotate the image when it gets displayed.
UIImageView *iv = [[UIImageView alloc] initWithFrame:self.frame.frame];
NSMutableString *path =[[NSMutableString alloc] initWithString: [[NSBundle mainBundle] resourcePath]];
[path appendString:#"/pic2.jpg"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *img = [UIImage imageWithData:data];
UIImage *fixed1 = [UIImage imageWithCGImage:[img CGImage]
scale:1.0
orientation: UIImageOrientationUp];
UIImage *sourceImage = img;
UIImage *fixed2 = [UIImage
imageWithCGImage:[img imageRotatedByDegrees:90].CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUp];
iv.image = fixed1; // fixed2;
[iv setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:iv];
In the end I took a different approach and made the UIImageView 360 wide and placed it in the centre and this achieved the desired effect.

UIWebView loads HTML from local file but fails occasionally

I've come across many ways to load and reload the content of a UIWebView from a local html file but it seems like the issue I have keeps coming back.
On my project I have a webView that dynamically loads HTML content pulled from one of the 50 different HTML files I have in my XCode project.
Depending on which button the user presses on the viewController before, a different HTML file is used to reload the content of my webView. Everything works fine most of the time but every now and then, the webView will simply display "(null)", after the webViewDidFinishLoad delegate method is called.
I don't understand what is going on, and when I go back and forth on this viewController and the webView is reloaded the HTML content ends up showing up properly eventually for the same HTML file.
Here is the code to load the HTML in the webView (called from the viewWillAppear method) :
- (void) reloadWebViewFromLocalFile{
// Reset the UIWebView
[self.contentWebView removeFromSuperview];
self.contentWebView = nil;
self.contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
[self.contentWebView setContentMode:UIViewContentModeScaleAspectFit];
[self.contentWebView setDelegate:self];
[self.contentScrollView addSubview:self.contentWebView];
[self.contentWebView release];
NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%d.%d",self.currentIndexPath.section+1,self.currentIndexPath.row+1] ofType:#"html"];
NSString *CSSContent = [NSString stringWithFormat:#"<style type=\"text/css\">body{padding-left:8px;padding-right:8px;font-family:\"%#\";}p{text-align:justify;}img{max-width:300px;display:block; margin:auto;}strong{font-family:\"%#\";}h1{font-size:20;font-family:\"%#\"}h2{font-size:18;font-family:\"%#\"}h3{font-size:17;font-family:\"%#\"}</style><script type=\"text/javascript\">touchMove = function(event) {event.preventDefault();}</script>", FONT_STAG_BOOK, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD];
self.HTMLString = [NSString stringWithFormat:#"<html><head>%#</head>%#</html>",CSSContent,[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]]];
[self.contentWebView loadHTMLString:self.HTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}
and this is the webViewDidFinishLoad delegate method :
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *stringContent = [self.contentWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
float height = [[self.contentWebView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"] floatValue] + 20;
[self.contentWebView setFrame:CGRectMake(0, 0, 320, height)];
[self.contentScrollView setContentSize:CGSizeMake(0, height+self.nextButton.frame.size.height)];
[self.nextButton setFrame:CGRectMake(0, height, 320, self.nextButton.frame.size.height)];
if ([self.contentWebView respondsToSelector:#selector(scrollView)]) {
self.contentWebView.scrollView.scrollEnabled = NO; // available starting in iOS 5
} else {
for (id subview in self.contentWebView .subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).scrollEnabled = NO;
}
}
Edit : Forgot to copy-paste the line that actually loads the HTML string in the webView
The problem seems to be using the bytes function, which doesn't work too well if the content encoding isn't exact...
[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]];
should be
[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileName] encoding:NSUTF8StringEncoding];

UIImageView setImage doesn't work

I'm making an animation calling repeatedly this function so I can make an animation:
The method where I have the issue, is a method of a UIImageView's subclass Class
#interface MyCustomImageView : UIImageView {
& it is the following:
- (void) animationShowFrame: (NSInteger) frame {
NSLog(#"frame: %d", frame);
if ((frame >= animationNumFrames) || (frame < 0))
return;
NSData *data = [animationData objectAtIndex:frame];
UIImage *img = [UIImage imageWithData:data];
NSLog(#"%#",img);
NSLog(#"data: %d", [data length]);
[self setImage:img];
}
The problem is that, it seems like the first image is perfectly set, but the rest of them are ignored...
NSLogging them I can see that the images are there & the data is different every time, so, they are loaded ok, even I tried to addSubview to self (the class is UIImageView's child), through a UIImageView & they are showing, but this is not the way cause in that case I'm using too much memory...
Asking for the description in the console, I can see how self.image = nil after setting it...
I don't know what else I can do with this, it's driving me crazy.
Try to subclass to a UIView instead to a UIImageView, and create the UIImageView as a property inside.
#interface CustomView : UIView {
UIImageView *imageView;
Then, in animationShowFrame use that imageView:
- (void) animationShowFrame: (NSInteger) frame {
NSData *data = [animationData objectAtIndex:frame];
UIImage *img = [UIImage imageWithData:data];
[self.imageView setImage:img];
}
Hope it helps!

Saving entire NSView contents, which is inside an NSScrollView

I'm trying to save contents of an NSView as an image, but, only the image visible inside the scrollview is saved.
Here is the actual image loaded in the view:
.
But this is how I'm able to save the image:
Is there any way to save the entire image in the NSView?
This is how I'm saving my NSView subclass:
- (void)save
{
[self lockFocus];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]];
[self unlockFocus];
NSData* data = [rep representationUsingType:NSPNGFileType properties:nil];
NSString* string = NSHomeDirectory();
NSString* pth1 = [string stringByAppendingPathComponent:#"ttx.png"];
[data writeToFile:pth1 atomically:YES];
}
This method is in the View i want to save.
Use -[NSView cacheDisplayInRect:toBitmapImageRep:]
NSBitmapImageRep* rep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
[self cacheDisplayInRect:self.bounds toBitmapImageRep:rep];

How can i scroll images dynamically

I m pretty new to iPhone development. I'm trying to implement scroll view with loading images dynamically from web. I m trying to use the following code.
for (counter = 0; counter < 2; counter++) {
RSSEntry *entry = [_allEntries objectAtIndex:counter];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: entry.articleUrl]];
UIImage *img = [UIImage imageWithData: imageData];
NSLog(#"%#", entry.articleUrl);
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
[imageData release];
CGRect rect = scrollViewController.frame;
imageView.frame = rect;
imageView.tag = (counter + 1); // tag our images for later use when we place them in serial fashion
[scrollViewController addSubview:imageView];
[imageView release];
}
These are two images that I load up on viewDidLoad. I want to load the next image when the user scroll onto the second image and if the user scroll next want to show that the image is loading. Any suggestion?
You muse use NSURLConnection and download the images asynchronously. You can use TCImageView
https://github.com/totocaster/TCImageView
It downloads the images in an async way.
I would also recommend Three20's TTImageView.