#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize NetImage;
#implementation PlayGameViewController
- (IBAction)imageView:(id)sender {
//Content of image should be received. But it not show on ImageView.
NSLog(#"\n Image == %# ",[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.so.cityhi.com/cometchat/plugins/filetransfer/download.php?file=559192_10152684963740198_1083006183_n.jpg"]]);
// NetImage is image View.
NetImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.so.cityhi.com/cometchat/plugins/filetransfer/download.php?file=559192_10152684963740198_1083006183_n.jpg"]]];
//It working good
//NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://so.cityhi.com/public/user/52/0052_37ea.png"]];
//[NetImage setImage:[UIImage imageWithData:data]];
NSLog(#"Success");
}
Simply If we want to use Image with URL.here is simply code and as you did is good.
NSString *ImageURL = #"YourURLHere";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
imageView.image = [UIImage imageWithData:imageData];
//imageView is Your UIImageView That you created in your .h file with IBOutlet
But when i use your given Image url (http://www.so.cityhi.com/cometchat/plugins/filetransfer/download.php?file=559192_10152684963740198_1083006183_n.jpg)
and requested on Browser then Image not showing that's why it may an issue with your image try another image as well.
Related
Hard to explain, but I'll try;
I am making an app for showing roadcameras. I have a TableViewController which leads to a DetailViewController.
What I want to achieve is that when I click on Row 1 it shows the picture from www.url1.com. If I click on Row 2, it shows the picture from www.url2.com, and so on. I can't seem to figure out how to do this.
I first started by using this code:
_place = #[#"E6 Minnesund",
#"E6 Heia"];
_url = #[#"http://webkamera.vegvesen.no/kamera?id=450848",
#"http://webkamera.vegvesen.no/kamera?id=100102"];
But stopped because I couldn't see how I could get this working...
Any ideas?
And please don't just rate the post down because you think its stupid. I'm kinda new to Xcode and for me, this is hard. Thanks in advance
I can post the whole project folder if necessary
just write the code in didselectrowAtIndexPath
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *picker = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
picker.row=indexPath.row;
[self.navigationController pushViewController:picker];
}
and in DetailViewController
in .h
#interface DetailViewController : UIViewController
{
}
#property(nonatomic,assign)int row;
in .m
- (void)viewDidLoad
{
dispatch_queue_t myqueue = dispatch_queue_create("myqueue", NULL);
// execute a task on that queue asynchronously
dispatch_async(myqueue, ^{
NSURL *url = [NSURL URLWithString:[_url objectAtIndex:row];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
Image.image = [UIImage imageWithData:data]; //UI updates should be done on the main thread
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:myImageView];
});
});
I'm new to cocoa and programming, sorry if this is something basic.
I'm using ARC. I have an NSImageView component, which is controlled by DropZone class. I drag & drop an image into it, but once I try to call in the scalling method I got, it tells me that the ": ImageIO: CGImageSourceCreateWithData data parameter is nil" I assume I'm doing something wrong, just don't know what yet.
Here's my method in DropZone.m
- (void)scaleIcons:(NSString *)outputPath{
NSImage *anImage;
NSSize imageSize;
NSString *finalPath;
anImage = [[NSImage alloc]init];
anImage = image;
imageSize = [anImage size];
imageSize.width = 512;
imageSize.height = 512;
[anImage setSize:imageSize];
finalPath = [outputPath stringByAppendingString:#"/icon_512x512.png"];
NSData *imageData = [anImage TIFFRepresentation];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil];
[dataToWrite writeToFile:finalPath atomically:NO];
}
The DropZone.h
#interface DropZone : NSImageView <NSDraggingDestination>{
NSImage *image;
}
- (void)scaleIcons:(NSString *)outputPath;
#end
And here's how I call it it my AppDelegate.m
- (IBAction)createIconButtonClicked:(id)sender {
NSFileManager *filemgr;
NSString *tempDir;
filemgr = [NSFileManager defaultManager];
tempDir = [pathString stringByAppendingString:#"/icon.iconset"];
NSURL *newDir = [NSURL fileURLWithPath:tempDir];
[filemgr createDirectoryAtURL: newDir withIntermediateDirectories:YES attributes: nil error:nil];
DropZone *myZone = [[DropZone alloc] init];
[myZone scaleIcons:tempDir];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Done!"];
[alert runModal];
}
the image get's pulled from the pastebaord:
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
if ([NSImage canInitWithPasteboard:[sender draggingPasteboard]]) {
image = [[NSImage alloc]initWithPasteboard:[sender draggingPasteboard]];
[self setImage:image];
[self setImageAlignment: NSImageAlignCenter];
}
return YES;
}
For some reason my "image" gets lost. Could somebody help?
The problem is that you're creating a new instance of DropZone in your app delegate, but I'm assuming that you created the image view in IB and changed its class to DropZone. Is that correct? If so, you need to have an IBOutlet in the app delegate connected to the image view in IB, and have this:
[self.iv scaleIcons:tempDir];
where iv is my IBOutlet.
Without knowing where "image" comes from, I would say your problem is in these lines...
anImage = [[NSImage alloc]init];
anImage = image;
You should just grab the image from the imageView...
anImage = [myImageView image];
I am fairly new to XCode - I'm designing an app with a single view that just displays a UIWebView that loads my jQuery mobile web site.
I have the Default.png and Default#2x.png files for the Launch images in place. When I run the app on my test device or in the emulator, it shows my Launch Image, and then flashes a white screen while the UIWebView loads the first page.
I know that I can change the background color and opacity so it will flash a different color than white, but I would like to prevent the flash altogether. I would like the app to launch, show the Launch image, and not show the UIWebView until the first page has completely loaded. Help?
# Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")
First, add the name-of-loading-image.png and name-of-loading-image#2x.png files to your Xcode project. The regular image should be 320 x 460, and the #2x image should be 640 x 920.
Then, in my ViewController.h file I added these properties:
#property (nonatomic, retain) UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
And then in my ViewController.m file I added this:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
#synthesize webView;
#synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"name-of-loading-image.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"name-of-loading-image.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
To know when your webView has finished loading, you must implement the UIWebViewDelegate protocol.
In your viewController's .h file:
#interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
#end
Then in your viewController's .m file, add this method:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
You can use the -viewDidLoad method in your viewController's .m to continue displaying your launch image. You can also begin the loading of content for your webView:
- (void)viewDidLoad {
[super viewDidLoad];
// continue to display launch image
UIImage *launchImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"launchImage.png" ofType:nil ]];
UIImageView *launchImageView = [[[UIImageView alloc] initWithImage:launchImage] autorelease];
[self.view addSubview:launchImageView];
// now setup the base view for the webView controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
// for rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//create a frame to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//determine the path the to the index.html file in the Resources directory
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
}
I currently have a mp4 video converted from a gif playing at app launch, but using a video stops playing music and has problems with airplay devices connected.
Anyways what i really want is showing my gif on app launch. But i can't seem to get the gif to "play" it only shows one frame, how do i make it play the whole gif ?
My current code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Show Animation;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Launch" ofType:#"mp4"]];
LaunchPlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
LaunchPlayer.view.frame = self.view.bounds;
LaunchPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
LaunchPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
LaunchPlayer.view.tag = 1;
[self.view addSubview:LaunchPlayer.view];
[LaunchPlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(LaunchFinish)
name:#"MPMoviePlayerPlaybackDidFinishNotification"
object:nil];
}
Although I don't see how the code you posted has anything to do with using a .gif, you its sticking to the first frame because iOS will not run an animated .gif. However, you can export each frame of your animation as a separate image and animate them like this.
-(void) viewDidLoad
{
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImageFrame1.gif"],
[UIImage imageNamed:#"myImageFrame2.gif"],
[UIImage imageNamed:#"myImageFrame3.gif"],
[UIImage imageNamed:#"myImageFrame4.gif"], nil];
imageView.animationDuration = 2.0;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
}
In my little example here. i used a UIWEBVIEW called webView and had spinning-loader.gif added as a resource. The image can me put anywhere in the project as long as its added. This showed the gif as well as made it animate. Hope this helps.
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"spinning-loader" ofType:#"gif"];
NSString* webViewContent = [NSString stringWithFormat:
#"<html><body><img style='width:85;height:85;' src=\"file://%#\" /></body></html>", pathImg];
[webView loadHTMLString:webViewContent baseURL:nil];
webView.scrollView.bounces = NO;
webView.scrollView.scrollEnabled = NO;
webView.opaque=FALSE;
[webView setBackgroundColor:[UIColor clearColor]];
How do I put a template'd background image on a text area?
Not sure what you mean with "template'd"... but....
In interface builder, add the imageview behind the textview, uncheck the "opaque" box for the textview.
You can set the contents property of the text views layer.
#import <QuartzCore/QuartzCore.h>
...
- (void) viewDidLoad {
[super viewDidLoad];
textView.layer.contents = (id)[UIImage imageNamed:#"myImage.png"].CGImage
}
should work
If you want to add the image programmaticaly I managed to do it this way:
First in my textviewViewController.h I added an outlet for the UITextView.
#import <UIKit/UIKit.h>
#interface textviewViewController : UIViewController {
UITextView *textView;
}
#property (nonatomic, retain) IBOutlet UITextView *textView;
#end
Next, in my .xib I added my UITextView and connected my outlet.
Lastly in the viewDidLoad of textviewViewController.m I add the image as a subview to my textview.
- (void)viewDidLoad {
[super viewDidLoad];
// Change the pathForResource to reflect the name and type of your image file
NSString *path = [[NSBundle mainBundle] pathForResource:#"d" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.textView insertSubview:imgView atIndex:0];
[imgView release];
}
I tried the insertSubview:atIndex: with both 0 and 1 with the same result, but I would stick with 0 idicating that it's behind the textview.