Objective-C NSData convert to MemoryStream - objective-c

I want to convert NSData to MemoryStream.
Example:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MRZ" ofType:#"cls"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
How do I convert fileData to MemoryStream?
Thanks.

Related

replace in NSURL

How can I do a string replace in NSURL?
I tried stringByReplacingOccurrencesOfString but it works with NSString.
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
I want to replace image url
http://example.com/image1.jpg
to
http://example.com/img1.jpg
NSURL has an absoluteString method that you could use like so
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
You could also directly operate on the NSString from the dataList as well:
NSString *urlString = [dataList[indexPath.item][#"image"] stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
NSString *webStringURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
imageURL = [NSURL URLWithString:webStringURL];
Hope This will help you.
You are going to replace the last path component of the URL so use the dedicated API of NSURL:
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSURL *newURL = [[imageURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:#"img.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL: newURL];
stringByReplacingOccurrencesOfString could cause unexpected behavior.

NSURL unused variable

I have generated a PDF file within my application which is saved to the Documents directory. I am trying to fetch the pdf to display within a UIWebView but i have an unused NSURL variable. Could this be the reason why the file is not loading properly? Here is my code which is attached to an IBAction button
The only way to get rid of that warning is to NSLog that NSURL. But I want to display that saved file.
This is the code I am using.
-(IBAction)pdf:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"client.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
NSLog(#"%#", webURL);
}
I figured it out. I needed to change my filePath NSString from stringByAppendingPathExtension to stringByAppendingPathcomponent. I can now view my PDF file in my UIWebView.

Unable to convert a string to a NSURL

I am trying to turn off WAL in CoreData using this code:
// Code to disable journaling mode
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlString = [applicationDocumentsDirectory stringByAppendingPathComponent: #"saori.sqlite"];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSDictionary *options = #{NSSQLitePragmasOption:#{#"journal_mode":#"DELETE"}};
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] init];
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:nil];
This is my NSString that I need to convert to a URL:
/Users/rolfmarsh/Library/Application Support/iPhone Simulator/7.1/Applications/7A614C51-AC51-4F5B-9716-6E3D2F160324/Documents/saori.sqlite
When I use that string in this statement, it generates nil.
NSURL *url = [[NSURL alloc] initWithString:urlString];
I don't see anything wrong with the string. What am I doing wrong?
Read the docs for NSURL. You have a file path and you need to create a file URL from the file path.
NSURL *url = [NSURL fileURLWithPath:urlString];

NSData to NSString with åöä

I'm downloading a webpage using NSMutableURLRequest but having problem putting that very webpage into a NSString.
NSString *username = #"my_username";
NSString *password = #"my_password";
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"https://www.mypage.com/login.php?username=%#&password=%#", username, password]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnPage = [NSString stringWithFormat:#"%.*s", [returnData length], [returnData bytes]];
This works fine except for special chars like åäö etc. Is there any better way?
Yes, use the following:
NSString *returnPage = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
That uses UTF8 instead of ASCII.

Convert NSData to a NSURL

I have seen this done the other way. But I have a NSData object returned that is suppose to be a URL of an image file.
How do I do this convert a NSData object into a NSURL?
Thanks
You first need to convert the NSData object to an NSString object and then you can just create a NSURL with the new string.
NSString *urlString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // Or any other appropriate encoding
NSURL *url = [[NSURL alloc] initWithString:[urlString autorelease]];