Receive Data from javascript - objective-c

i have a stupid question...sometimes i see developers use the following code...
NSString *newURL = [_parameters objectForKey:#"url"];
when i use this i get error... here what is _parameters and how to declare it??
i can provide more example..
NSLog(#"SaveImage Called");
//google code for turning base64 into uiimage
NSURL *url = [NSURL URLWithString:[self._parameters objectForKey:#"imageData"]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imageData];
/*
if(img != nil) {
UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);
}
*/

_parameters is a NSDictionary that seems to be defined previously in the code, e.g.
NSDictionary _parameters = [NSDictionary dictionaryWithObjects:#"foo", #"bar", nil
forKeys:#"url", #"host", nil];
What exactly is your question? How is this related to JavaScript (edit: question was tagged as javascript)? You know that the language you use is Objective-C ?

Related

objective-c google static map returns nil

this is my code:
#define GOOGLE_STATIC_MAP_IMAGE #"https://maps.googleapis.com/maps/api/staticmap?center=%#&zoom=15&size=320x150&maptype=roadmap&markers=color:green|label:H|%#"
-(UIImage*)GetGoogleStaticMap
{
NSString* coordinates = [NSString stringWithFormat:#"%f,%f", latitude, longitude];
NSString *stringURL = [NSString stringWithFormat:GOOGLE_STATIC_MAP_IMAGE, coordinates, coordinates]; //coordinates x2 = for the center & the marker
NSURL *url = [NSURL URLWithString:stringURL];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
if I paste stringURL to the browser i get the desired result, but imageData returns nil - ideas?
It may have returned nil because of certain characters in the string.
Try calling stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding on your stringURL before creating the url object.
For example:
newStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

load data from cache with NSURL doesn't work

i'm trying to load a photo from my cache directory.
this is the code where i write the photo into a cache folder:
+(BOOL)writeAData:(NSData *)imageData atURL:(NSURL *)fileUrl
{
return [imageData writeToURL:fileUrl atomically:YES];
}
this code works fine, but i have problems when i try to load the code from the cache folders:
i use the method initWithContentsOfURL inside a block:
NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageUrl];
this is the format of url
file://localhost/Users/MarcoMignano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/0E8E54D8-9634-41CF-934B-A7315411F12F/Library/Caches/image_cache/Meyer%20Library.jpg
the URL is correct, but when i try to use the method above i get this error:
..... [__NSCFString isFileURL]: unrecognized selector sent to instance 0x8a0c5b0 .....
that is the code when i take the url of my model:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.photos = [PhotoAlreadyDisplayed getAllPicture];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([sender isKindOfClass:[UITableViewCell class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if (indexPath) {
if ([segue.identifier isEqualToString:#"Show Image"]) {
if ([segue.destinationViewController respondsToSelector:#selector(setImageUrl:)]) {
NSURL *url = [[self.photos objectAtIndex:indexPath.row] valueForKey:#"url"];
[segue.destinationViewController performSelector:#selector(setImageUrl:) withObject:url];
[segue.destinationViewController setTitle:[self titleForRow:indexPath.row]];
}
}
}
}
}
whats wrong with the URL? how can i fix it? thank you
i have find the solution:
when i load the url for the model, i need to initialize the url using fileURLWithPath method:
NSURL *url = [NSURL fileURLWithPath:[[self.photos objectAtIndex:indexPath.row] valueForKey:#"url"]];
Now all works fine, thank you so much for yours help
[NSData initWithContentsOfURL:] is expecting an NSURL as the argument passed in. It looks like you might be passing in a string, rather than an NSURL. Is this the case?
EDIT: Just humour me - try replacing
NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageUrl];
with
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.imageUrl]];
What happens?
Though it doesn't look like you're returning successfully from initWithContentsOfURL:, but the snippet below may be of use to others:
NSError *error;
NSData *data = [[NSData alloc] initWithContentsOfURL:TestURL options:0 error:&error];
if(data) {
NSLog(#"Success");
}
else {
NSLog(#"Failed: %#", Error);
}
solution: when i load the url for the model, i need to initialize the url using fileURLWithPath method:
NSURL *url = [NSURL fileURLWithPath:[[self.photos objectAtIndex:indexPath.row] valueForKey:#"url"]];
Now all works fine, thank you so much for yours help

Objective C, Can't load an image from data returned by ASIHTTP request

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSData *data = [NSData dataWithData:[request responseData]];
NSData *responseData = [request responseData];
UIImage *img = [[UIImage alloc ]imageWithData:data];//it blows up here
self.q.image = img;
[img release];
self.request = nil;
[delegate imageDidLoad:self.indexPathInTableView];
}
I am downloading an image data using ASIHTTP request and it worked fine. But If I try to create an image using the data from ASIHTTP request then it blows up..What is the problem?
Thanks in advance...
Its because you are using imageWithData: to create your image, you should use initWithData: or just [UIImage imageWithData:data] instead of alloc'ing first.
Your code doesn't work because imageWithData: is a static method on UIImage that generates an autoreleased UIImage instance. Because it is a static method, not an instance method, it isn't a recognised selector on the UIImage instance you get when you call [UIImage alloc]
as the error says the it's an unrecognized selector.
you should call - (id)initWithData:(NSData *)data.
[[UIImage alloc] initWithData:data];
or
[UIImage imageWithData:data];

Having trouble with this NSURL method

Can anybody tell me excatly how I should execute this method on an NSURL object?
URLByResolvingSymlinksInPath
Compiler says that the method can't be found when I execute it.
Thanks a lot.
This is where I am trying to implement the code:
- (void)data
{
NSURL *url = [[NSURL alloc] initWithString: [[user data] objectAtIndex: 5]];
NSURL * url2 = [[NSURL alloc] init];
url2 = [url URLByResolvingSymlinksInPath];
NSData *newImage = [[NSData alloc] initWithContentsOfURL:url2];
NSImage *image = [[NSImage alloc] initWithData:newImage];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 3.0;
[imageView setImage:image];
[user autorelease];
}
Assuming that you have NSURL named something like myURLWithSymlinks, you would do something like:
NSURL * myURLWithoutSymlinks = [myURLWithSymlinks URLByResolvingSymlinksInPath];
Note that according to the docs, URLByResolvingSymlinksInPath is only available in iOS4.0 or above and MacOSX 10.6. If you are using an older version of iOS (or MacOSX), that could be the cause of your problem.

Getting Image from URL Objective C

I'm trying to get an image from an URL and it doesn't seem to be working for me. Can someone point me in the right direction?
Here is my code:
NSURL *url = [NSURL URLWithString:#"http://myurl/mypic.jpg"];
NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
cell.image = [UIImage imageNamed:newIMAGE];
When I debug the newIMAGE string is nil so something isn't working there.
What you want is to get the image data, then initialize a UIImage using that data:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
cell.image = [UIImage imageWithData: imageData];
[imageData release];
As requested, here's an asynchronous version:
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
cell.image = [UIImage imageWithData: data];
});
[data release];
});
Ok there's a couple of things wrong here:
The conversion from URL (url) to NSString (newImage) is incorrect, what the code actually does there is try to load the contents of "http://myurl/mypic.jpg" into the NSString.
The -imageNamed method takes a string that is a path of a local file, not a URL as an argument.
You need to use an NSData object as an intermediary, like in this example:
http://blogs.oreilly.com/digitalmedia/2008/02/creating-an-uiimage-from-a-url.html
the accepted answer asynchronous version worked very slow in my code. an approach using NSOperation worked light years faster. the code provided by Joe Masilotti --> objective - C : Loading image from URL? (and pasted below):
-(void) someMethod {
// set placeholder image
UIImage* memberPhoto = [UIImage imageNamed:#"place_holder_image.png"];
// retrieve image for cell in using NSOperation
NSURL *url = [NSURL URLWithString:group.photo_link[indexPath.row]];
[self loadImage:url];
}
- (void)loadImage:(NSURL *)imageURL
{
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(requestRemoteImage:)
object:imageURL];
[queue addOperation:operation];
}
- (void)requestRemoteImage:(NSURL *)imageURL
{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self performSelectorOnMainThread:#selector(placeImageInUI:) withObject:image waitUntilDone:YES];
}
- (void)placeImageInUI:(UIImage *)image
{
[self.memberPhotoImage setImage:image];
}
In Swift 3 and 4
let theURL = URL(string:"https://exampleURL.com")
let imagedData = NSData(contentsOf: theURL!)!
let theImage = UIImage(data: imagedData as Data)
cell.theImageView.image = theImage
This will be done in the main thread.
And to perform the same in asynchronous/background thread
DispatchQueue.main.async(){
let theURL = URL(string:"https://exampleURL.com")
let imagedData = NSData(contentsOf: theURL!)!
let theImage = UIImage(data: imagedData as Data)
}
cell.theImageView.image = theImage
Updating upon Jim dovey answer,[data release] is no longer required because in the updated apple guidelines. Memory management is done automatically by ARC (Automatic counting reference) ,
Here is the updated asynchronous call,
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"your_URL"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
self.your_UIimage.image = [UIImage imageWithData: data];
});
});