I've created a simple XML-Parser to parse my rss Feeds to my app.
The id, currentstatus, picture of the simple to parse from the xml file.
But I can't get the image from the XML file. . i retrieve the all the images from xml file. but the noimage.jpg not displaying. could you please any help me.
XML code
<Agent>
<id>3422</id>
<currentstatus>Logged Off</currentstatus>
<picture>1</picture>
</Agent>
<Agent>
<id>3432</id>
<currentstatus>Logged Off</currentstatus>
<picture>0</picture>
</Agent>
i tried the following code:
UIImage * cellimages = [[UIImage alloc]init];
NSString *string1 = #"http://telecomstats.co.uk/images/LLReaderProfile/";
NSString *cellvalue =[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]picture];
NSString* disable = [[NSString alloc] initWithString:#"0"];
if (cellvalue == disable)
{
cellimages = [UIImage imageWithContentsOfFile:#"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"];
}
else {
cellimages = [string1 stringByAppendingString:[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]id]];
cellimages = [cellimages stringByAppendingString: #".jpg"];
}
cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]];
cell.imageView.image = cellimages;
Does someone can help me?
Thanks for any help and insight.
You're using cellimages which is an UIImage object to store strings in your else-branch. Also when cellvalue == disable is true, you execute this:
cellimages = [UIImage imageWithContentsOfFile:#"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"];
cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]];
There you put an UIImage in [NSURL URLWithString:] which probably will not work; actually this code should produce quite some warnings. Try to properly separate between the URL string and the image object to display. Put all URL stuff in an NSString and get the UIImage afterwards when you have determined the correct URL.
Note that your code has some other problems, especially that you are using dataWithContentsOfURL and imageWirhContentsOfFile both of which will block until the image is retriebes. This will result in application hangs when using a bad internet connection and if the network is too slow, will crash your app. Have a look into asynchronous downloads and using frameworks like AFNetworking.
Related
I am building a facebook app. Where I have to show my friends profile picture. But the image is not showing all the time when I run my project. I have used the code like tha
NSString *url = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture? type=large",friendId];
NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageUrl]];
imgView.frame = CGRectMake(100.0f,100.0f,200,200);
[self.view addSubview:imgView];
But when I am printing the url in log then it shows an url and if I go with this url in browser then It shows picture in the browser.
For printing the URL I used the code below:
NSLog(#"%#",url);
right ive got this code below which will save a integer when the save button is clicked and the app is shut however i need it to do this automatically... how do I go about doing this with ios? quite new to the whole apple coding thing so any help is welcome
-(IBAction)SaveNumber:(id)sender{
[[NSUserDefaults standardUserDefaults] setInteger:Number forKey:#"savenumber"];
}
- (void)viewDidLoad {
Number = [[NSUserDefaults standardUserDefaults] integerForKey:#"savenumber"];
ShowSavedNumber.Text = [NSString stringWithFormat:#"%i",Number];
[super viewDidLoad];
}
The problem is, where you try to save this value? you don't have a file to storage this value in Documents directory inside the App.
You can save any value in your app, but you have to use a file for storage and read, or saving and load via URL on your server. Like file.plist, SqlLight, json with MySql.
So the first simple saving is using a file .plist inside your app, under the AppDelegate.m you have to check your directory if the file exist and copy inside a Documents directory (the only writable folder), but after that you have to learn how to load and save on the same file using Dictionary , MutableDictionary and some other class. I can help you to understant the first steps like a check and create a .plist, read, and first base to save, but for other thing is to much code and yopu have to watch other tutorials.
So create a .plist file and change the structure like this:
Change key has you want, but remember the Dictionary contain more string value, if you want add more Item you have to add other row inside the same Root Array.
Root = Array
Item0 > some string with value
Item1 > some string with value
Now you have to copy this file inside a writable Documents folder when the application lauching, so inside your AppDelegate.m create a function:
- (void)createPlistFile{
NSError *error;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"yourFile.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"yourFile" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
}
Under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { add a call: [self createPlistFile];
Ok now you have Check first if the file exist in documents folder, and if is false, the app create a new file.
Now here the standard code to read a new file inside your app:
in your ViewControlle.h write:
#interface ViewController : UIViewController {
NSMutableArray *loadData;
}
#property (nonatomic, strong) NSMutableArray *loadData;
Come back to your ViewController.m and write:
#synthesize loadData;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"yourFile.plist"];
NSMutableArray *dataDict = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
self.loadData = dataDict;
Ok now you have create a new PLIST file inside a Documents Directory an correctly load, but now you have to save, this is a basic method to save, but theres really many mode for that and I can't write all here ;)
- (IBAction)save:(id)sender {
NSMutableDictionary *nr2 = [[NSMutableDictionary alloc] init];
[nr2 setValue:emittente.text forKey:#"plistKey"];
[nr2 setValue:link.text forKey:#"plistKey"];
[nr2 setValue:images.text forKey:#"plistKey"];
[nr2 setValue:web.text forKey:#"plistKey"];
[YouMutableDict addObject:nr2];
[YouMutableDict writeToFile:listFav atomically:YES];
}
Ok that's all I hope this help you to start understand how to save Data on app if yes please vote ;)
Changes made to NSUserDefaults are not automatically saved. You must call -synchronize on the NSUserDefaults object in order to save. Generally you want to do this once after you have made all of the updates you plan on performing (if in fact you need to do multiple updates).
Here is Apple's reference: http://developer.apple.com/library/ios/ipad/#documentation/cocoa/reference/foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
Can someone explain to me how cocos2d uses png as a font?
I would like to do something similar as my font only contains numbers.
if you do a global search on the string fps_images.png , your IDE should take you real close to the following lines in cocos CCDirector class (version 2.0) :
FPSLabel_ = [[CCLabelAtlas alloc] initWithString:#"00.0" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
SPFLabel_ = [[CCLabelAtlas alloc] initWithString:#"0.000" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
drawsLabel_ = [[CCLabelAtlas alloc] initWithString:#"000" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
then look up CCLabelAtlas. Your image must be for a fixed width font.
If you want to reuse the same image included to make something other than the FPS display the following code should work:
CCLabelAtlas *scoreLabel = [[CCLabelAtlas alloc] initWithString:#"00.0" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
Then to set it to the numbers you want something like this works:
[scoreLabel setString:[NSString stringWithFormat:#"%d", (int)score]];
I am creating a game of questions and answers for iphone using cocos2d, and I wonder how can I do to display the question on the screen using ccLabel seeking data from a plist. can someone help me with this!
What you want to do is grab it from your resources bundle, and save it into a dictionary. From there you have access to every value inside of your plist. That can be achieved with something like this:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"myList.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
Then when you're ready to display the question just use the [plistData objectForKey: ] method. Do you need help setting up the label also?
Sure man. You want to set it up like this:
CCLabel* questionLabel = [CCLabel labelWithString:#"Your Question"
fontName:#"Marker Felt" fontSize:64];
CGSize size = [[CCDirector sharedDirector] winSize];
label.position = ccp( size.width /2 , size.height/2 );
[self addChild: questionLabel];
That'll display your label in the middle of the screen. You can change the position, font, etc. The way you access your data from the plist is determined by exactly how you set it up. But using the technique I gave your earlier you shouldn't have a problem.
I want to play mp3 files on my server with AVAudioPlayer
I've tried this code, but it does not work.
-(IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:#"http://www.domain.com/audio/test.mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
NSBundle can only select the files in the apps bundle, not the ones on the internet.
Make this the path
NSString *path = #"http://www.domain.com/audio/test.mp3";
and leave everything else the same and it should work
try this:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audio play];