I am trying to load the bytes of an image like this:
NSURL *img = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"img" ofType:#"png"] isDirectory:NO];
NSString * test = [NSString stringWithContentsOfFile:[img absoluteString] encoding:NSASCIIStringEncoding error:&err];
But I always get following error:
Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn\u2019t be completed.
(Cocoa error 260.)" UserInfo=0xc02abf0
{NSFilePath=file://localhost/Users/admin/Library/Application%20Support/iPhone%20Simulator/4.3.2/Applications/C55551DB-152A-43D6-A1E0-9845105709D6/myApp.app/img.png,
NSUnderlyingError=0xc02ccc0 "The operation couldn\u2019t be completed. Folder or file does not exist"}
However the file DOES exist, and if I copy/paste the NSFilePath into the browser it finds the image. What could be wrong?
Why not
NSString *path = [[NSBundle mainBundle] pathforResource:#"img" ofType:"png"];
NSData *data = [NSData dataWithContentsOfFile:path];
You can't just store random bytes in an NSString - it will try to convert them into a string and might fail. You need to use an NSData object for binary data.
You also don't need to use NSURLs at all; NSBundle will give you a path as a string.
As you are using the file URL, use [img path] instead of [img absoluteString] in the second line.
Or use [NSURL URLWithPath] in the first line.
Store bytes in NSData:
NSData *bytes = UIImagePNGRepresentation([UIImage imageNamed:#"myImage.png"]);
NSData* imageData = UIImagePNGRepresentation("imagename.."); //the image is converted to bytes
Related
I'm new on IOS development and i'm working on a pdf application and i need to store a PDF file on a NSData variable, I have the PDF path but i get this message error when i try to put this pdf on the NSData variable using dataWithContentsOfFile her is my simple code :
NSError *error;
NSString *PdfPath = [NSString stringWithFormat:(#"%#"),document.fileURL ];
NSString *newPath = [PdfPath stringByReplacingOccurrencesOfString:#"file://localhost" withString:#""];
NSLog(#"OriginalPdfPath => %#", newPath);
NSData *pdfData = [NSData dataWithContentsOfFile:newPath options:NSDataReadingUncached error:&error];
NB : the pdf path is in this format : /Users/bluesettle/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/BBEF320E-7E2A-49DA-9FCF-9CFB01CC0402/ContractApp.app/Pro.iOS.Table.Views.pdf
thanks for your help
Cocoa error 260 is a NSFileReadNoSuchFileError (as listed in FoundationErrors.h), meaning the file could not be found at the path you specified.
The problem is that your path still contains encoded spaces (%20), because you're basing it on the URL. You can simply do this:
NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path]];
Try to use NSBundle
NSString *newPath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"]
Edit:
Than you can use bundleWithPath method, here is an example:
NSString *documentsDir= [NSString stringWithFormat:#"%#/Documents", NSHomeDirectory()];
NSString *newPath= [[NSBundle bundleWithPath:documentsDir] bundlePath];
I want to send json service when the user search text on searchbar. Here the issue is that I return null value of NSData object, what the issue is here? If I define the same url which I print in console that works but what's the issue here?
-(void)doIt{
NSURL *url = [NSURL URLWithString:weburls];
NSData *data =[NSData dataWithContentsOfURL:url];
[self getData:data];
}
If I will write like that then it works, but I want to call the service on the searchbar event but there is a problem
NSString *weburl = [NSString stringWithFormat:#"%#%#",
#"http://192.168.1.196/ravi/iphonephp?mname=",searchText];
NSLog(#"%#",weburl);
NSURL *url = [NSURL URLWithString:weburl];
NSLog(#"the url is : %#",url);
NSError *error;
NSData *data =[NSData dataWithContentsOfURL:url options:nil error:&error];
NSLog(#"Data is :%#",data);
NSLog(#"the Error massage is : %#",error);
[self getData:data];
Gives me console value like
customCellDemo[1553:f803] the url is : http://192.168.1.196/ravi/iphonephp?mname=a
2012-03-16 15:26:36.259 customCellDemo[1553:f803] Data is :(null)
2012-03-16 15:26:43.624 customCellDemo[1553:f803] the Error massage is : Error
Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"
UserInfo=0x6ab2760 {NSURL=http://192.168.1.196/ravi/iphonephp?mname=a}
From the manual of dataWithContentsOfURL;
Return Value: A data object containing the data from the location
specified by aURL. Returns nil if the data object could not be
created.
In other words, it can either not create an NSData (unlikely) or it can probably not get any data from your supplied URL. I suggest you try to use dataWithContentsOfURL:options:error: instead to get an error code and be able to diagnose the problem.
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:currentVideoDownload];
filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[responseData writeToFile:filePath atomically:YES];
NSError *error;
NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:&error];
NSLog(#"Length:%d Error:%#",[mediaData length],[error localizedDescription]);
LOG value: Length:0 Error: The operation could not be completed.
(Cocoa error 60)
Data is saving on file path properly but while fetching data from same path getting zero.
Thanks in advance.
The issue is that you are not writing to a writable file path. This is most likely because you are escaping the file path, which is not necessary, and in fact could cause IO reads/writes to fail if the escaped path does not exist. Percent escapes should only be used for HTTP requests, or related NSURL operations. Try removing the line:
filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I have a URL with tag:
http://ws.cdyne.com/WeatherWS/Images/sunny.gif
When I do like this:
NSURL *url=[NSURL URLWithString:#"http://ws.cdyne.com/WeatherWS/Images/sunny.gif"];
it's getting image in gdb and I am able to download from this URL. But when I write code like this:
NSURL *url=[jsonItem objectForKey:#"PictureURL"];
(where jsonItem is dictionary and passing it to data and then to UIImage), then it shows an exception.
When I get the value of:
[jsonItem objectForKey:#"PictureURL"];
in gdb then it shows like:
2011-03-15 11:08:16.405 XML[1576:20b] jsonURKL ..... ...
http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif (in next line in gdb)
I need to download through this:
NSURL *url=[jsonItem objectForKey:#"PictureURL"];
I am not able to do that. What could be wrong?
Url itself one string.
so you have to use below code
NSURL *url=[NSURL URLWithString:[jsonItem objectForKey:#"PictureURL"]];
NSData *data = [NSData dataWithContentsOfURL: url];
now setimgge with [UIImage imageWithData: data]; this code
please do this in following way,
NSURL *url=[NSURL URLWithString:[jsonItem objectForKey:#"PictureURL"]];
NSData *data = [NSData dataWithContentsOfURL: url];
cell.iconImage.image=[UIImage imageWithData: data];
i want to take some xml data from local path,and to parse,but when i use following code
NSLog returns(content) different texts which is differed from xml file, how can i get exact xml data to check ,it consists correct xml data or not? any help please? when i parse , it returns nothing..i have saved the file as .xml and copied to local resource folder?
NSString *xmlFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"samp.xml"];
NSString *xmlFileContents = [NSString stringWithContentsOfFile:xmlFilePath];
NSData *data = [NSData dataWithBytes:[xmlFileContents UTF8String] length:[xmlFileContents lengthOfBytesUsingEncoding: NSUTF8StringEncoding]];
NSString *content=[[NSString alloc]
initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
NSLog(#"%#",content);
This is almost assuredly an encoding problem. Make sure your xml file is in UTF8 or convert it to UTF8 before you try to create the NSData object. Once that's done, the following code produces the same output as input.
NSString *open = [NSString stringWithContentsOfFile: [#"~/Desktop/note" stringByExpandingTildeInPath] encoding: NSUTF8StringEncoding error: NULL];
NSData *data = [NSData dataWithBytes: [open UTF8String] length: [open lengthOfBytesUsingEncoding: NSUTF8StringEncoding]];
NSString *save = [NSString stringWithUTF8String: [data bytes]];
[save writeToFile: [#"~/Desktop/note2" stringByExpandingTildeInPath] atomically: NO encoding: NSUTF8StringEncoding error: NULL];
You'll probably want to use one of the NSXML classes, unless you want to do all of the parsing yourself.