UIImage conversion from NSData - uiimage

I am creating an app that takes a json and parse it in order to obtain various NSDictionary filled by NSString.
When I start to show all these data inside my table view, Name and Address work fine, but I can't show up images, I actually use this brunch of code:
NSLog (#" URL img --> %#" , [dict objectForKey:#"urlimgp"]);
p.image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dict objectForKey:#"urlimgp"]]]];
NSLog (#" IMG --> %#" , p.image);
I can't figure out why the output from the first NSLog shows the exact url (so i know everything works fine with sql and php), but in the second NSLog I have got a (null). I really thank you in advance!

Your URL is not being encoded with percent escape encoding using UTF-8..
NSString *urlStr = [dict objectForKey:#"urlimgp"];
urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [[[NSData alloc] initWithContentsOfURL:url] autorelease];
UIImage *image =[UIImage imageWithData:data];

Related

MPMoviePlayerController failed with itemFailedToPlayToEnd

I have retrieved movies file from my iPhone Photo Library, and saved them into my App`s Documents directory. When I click one movie file, I want to use MPMoviePlayerController to play it. Unfortunately, It fail to play the movie file with such error:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
I have searched for a long time to resolve this problem. Somebody say the format maybe is unsupport. The movies file is retrieved from Photo Library, so the format should be OK. Also, I copy the file to NSBundle and MPMoviePlayerController can play it. So the file is OK. Following is my codes:
NSString *newstr = [mFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
DLog(#"the raw is %# the newstr is %#", mFilePath, newstr);
NSURL *url = [NSURL URLWithString:newstr];
DLog(#"the url is %#", url);
#if 0
NSBundle *bundle = [NSBundle mainBundle];
if (bundle) {
NSString *path = [bundle pathForResource:#"test" ofType:#"MOV"];
if (path) {
url = [NSURL fileURLWithPath:path];
DLog(#"the new url is %#", url);
}
}
#endif
mPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[mPlayer setMovieSourceType:MPMovieSourceTypeFile];
[mPlayer prepareToPlay];
[mPlayer.view setFrame:self.view.bounds];
[mPlayer play];
[self.view addSubview:mPlayer.view];
Changing:
NSURL *url = [NSURL URLWithString:path];
to
NSURL *url = [NSURL fileURLWithPath:path];
fixed it for me, then:
mPlayer.contentURL = url;
[mPlayer prepareToPlay];
but you already do this correctly. I create the mPlayer once and then just shift the contentURL about (instead of alloc + initWithContentURL) for different videos. Have you tried that?

Displaying Images in Image Well

I am a beginner trying to display an image, bitmap and tiff have been tried, in an image well or a button but with no success. The code is below. It runs but nothing is displayed. I have tried two ways of defining the string and also using self.well or _well but nothing works. If I try to use [well1 setImage:testImage]; as is shown in other examples I get an error "use of undeclared identifier.
//Code is
NSString *inFilePath = #"/Documents/Track1bmp.bmp";
NSImage *testImage = [[NSImage alloc] initWithContentsOfFile:inFilePath];
//well1 is your outlet
[self.well1 setImage:testImage];
[self.butt1 setImage:testImage];
[_well2 setImage:testImage];
// Also trying this
NSString* imageName = [[NSBundle mainBundle]
pathForResource:#"/Applications/Sudoku4/Pinktiff" ofType:#"TIFF"];
NSImage* tempImage = [[NSImage alloc] initWithContentsOfFile:imageName];
[self.well1 setImage:tempImage];

Load .png into xcode and convert to UIImage

I'm completely new to Xcode, obj-c etc, but I want to use Parse.com to save an image to their server, they say you have to use this line, to convert a UIImage to NSData
NSData *imageData = UIImagePNGRepresentation(image);
How would I load one of the .png's that is in my resource folder and then convert it to a UIImage? I'm also using the Sparrow Framework if that helps any.
EDIT
I've tried this code as suggested below.
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"terrainHexAtlas_v1#2x.png" ofType:#"png"];
UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];
NSData *imageData = UIImagePNGRepresentation(myImage);
PFFile *imageFile = [PFFile fileWithName:#"terrainHexAtlas_v1#2x.png" data:imageData];
[imageFile save];
But nothing seems to be happening. I tried a breakpoint at the last line and the vars imagePath, myImage, imageData are all 00000000 which I don't get.
you should follow code:
//png
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image" ofType:#"png"];
UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];
NSData *pngImageData = UIImagePNGRepresentation(myImage);
//jpeg
NSString *imagePath2 = [[NSBundle mainBundle] pathForResource:#"your_image" ofType:#"jpg"];
UIImage *myImage2 = [UIImage imageWithContentsOfFile:imagePath2];
NSData *jpegImageData = [UIImageJPEGRepresentation(myImage2, 1.0f)];
Do not contain an extension in pathForResource. check following mistake case.
// not contain an extension
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image.png" ofType:nil];
// contain an extension
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image" ofType:#"png"];
// some people mistake case. wrong code. that return nil
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image.png" ofType:#"png"];

Convert NSDictionary array values into NSString format for MFMailComposeViewController

I have a UIPickerViewDelegate that allows the user to select a term from the picker and see the term's definition by using NSDictionary to access data stored in a plist file. I also have a MFMailComposeViewController to allow the user to email the term and it's definition elsewhere.
But I can't seem to get the term and definition formatted properly for entry into the email. I looked at various "convert NSDictionary to NSString" solutions, but none seem to offer what I need, including variations of Term = [dict objectForKey:#"Term"].
Under viewDidLoad, I have the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Glossary" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.termArray = [dict objectForKey:#"Term"];
self.definitionArray = [dict objectForKey:#"Definition"];
My pickerView code includes this:
NSString *resultString = [[NSString alloc] initWithFormat:
#"%#",
[definitionArray objectAtIndex:row]];
definitionLabel.text = resultString;
NSString *termString = [[NSString alloc] initWithFormat:
#"%#",
[termArray objectAtIndex:row]];
The code for showEmail contains:
int definitionIndex = [self.termPicker selectedRowInComponent:0];
NSString *emailTitle = [NSString stringWithFormat:#"Definition of %#", termLabel];
NSString *emailDefinition = [definitionArray objectAtIndex:definitionIndex];
NSString *messageBody = [NSString stringWithFormat:#"The definition of <B>%#</B> is:<P> <B>%#</B>", termLabel, emailDefinition];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
I would like the email title and message body to pull from the Term selected in the picker and the Definition associated with it. I'm sure it must be a simple formatting issue, but I can't figure it out.

copy and paste text + image from NSPasteboard

we are working on a C++ Qt applciation that copies selected text and/or images from external applications, modifies it and then paste it back. Since we are on Mac, we are doing this part with Objective-C.
We are having problems trying to get an image from the Pasteboard. It works fine for text, but we are not sure about how to handle images or combination of text+image.
Since we dont know what the user might select, we should be able to perform a generic retrieval of content of the pasteboard to modify it and putting it back in the pasteboard.
We've try this:
//we thought about retrieving some generic item from pasteboard, using NSPasteboardItem
NSArray *classes = [[NSArray alloc] initWithObjects:[NSPasteboardItem class], nil];
NSDictionary *options = [NSDictionary dictionary];
NSArray *auxArray = [[NSPasteboard generalPasteboard] readObjectsForClasses:classes options:options];
NSData *archived_data = [auxArray objectAtIndex:0];
Our solution for handling text was:
NSString *text = [[NSPasteoard generalPasteboard] stringForType:NSStringPboardType];
string text_str = string([text UTF8String]);
It didnt work, so, How can we get the user selection from the pasteboard?
We need to get the raw bytes or rtf content in order to modify it as we need, and then putting it back in the pasteboard and paste it back replacing the original user selection.
Thanks!
I think this function will help you
- (IBAction)paste:sender
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSImage class]];
NSDictionary *options = [NSDictionary dictionary];
BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
if (ok)
{
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
NSImage *image = [objectsToPaste objectAtIndex:0];
[imageView setImage:image];
}
}

Resources