I'm trying to post an image to TwitPic.com using their API.
However, I have never posted an image using HTTPPOST or however else before.
Anybody enlighten me to how I can post NSData from a UIImage using their api?
TwitPic is expecting a multi-part form data. There is a great open source library called ASIHTTPRequest.
You can use their APIs and post your image as multi-part/form-data.
See below for the sample.
http://allseeing-i.com/ASIHTTPRequest/How-to-use
Use the UIImagePNGRepresentation function or its JPEG equivalent to turn a UIImage into an NSData:
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImagePNGRepresentation
#Jamie:
Try:
[request setData:twitpicImage forKey:#"media"];
If the image view is actually displaying an image that is stored in a file on disk, use:
[request setFile:theImagePath forKey:#"media"];
This way, ASIHTTPRequest will stream the image data from disk, so you don't have to keep an NSData instance in memory.
Ta
Ben
Instead of setPostValue for the image, use setData instead.
Related
What I am trying to do is get the UIWebViews full content (Text, images, and CSS) in one litte package.
I figured NSData would be the best way to do this. Or is there another more compressed version?
Probably late enough to respond to this, but I figured out you can get all the data from the request you made with NSCachedURLResponse as such:
NSCachedURLResponse* response = [[NSURLCache sharedURLCache]
cachedResponseForRequest:[webView request]];
NSData* data = [response data];
I have an application where I am sending image data on server through ftp and retrieving back in a functionality of preview. Now I am able to send the image on server,but while retrieving it back,I am getting following error in NSData and 0 bytes are fetched.
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn\u2019t be completed. (Cocoa error 256.)"
I am parsing data through XML and fetching the image url like this
http://111.222.333.44/abc/images/User/img102-13.png
Now,when I use the following code to retrieve imgstr to nsdata, but getting the above error in the same.
NSURL *u = [NSURL URLWithString:imgstr];
NSError *error;
NSData *dt = [NSData dataWithContentsOfURL:u options:NSDataReadingMapped error:&error];
myImage = [UIImage imageWithData:dt];
I am able to fetch other data easily,but not the image data. Can anybody point out,where I am doing mistake or how I can resolve the problem? Thanks in advanced.
EDIT :
In my application I am first of all capturing an image and storing it on the server. And then on the 2nd page,I need to fetch the image data and to show the same image when the user clicks a button called Preview. I am having a web service where I am getting the data including the path of the image as shown above,but with the method shown I am not able to fetch the Image data in NSData and getting the Cocoa error 256. Hope this help someone to show me a better and correct Path. Thanks.
I think the problem with this approach is that it will block the UI thread until the image is downloaded.
try to load that image in separate thread.
Well i tried your code and i too have a webService that takes the image from a url.
instead of doing the above code. you can directly do
try this:
NSURL *u=[NSURL URLWithString:imgstr];
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:u]];
In my case its working.
If you are still struck then the below link might help you:
http://blog.beefyapps.com/2010/03/coredata-sqlite3-database-constraint-failed-error/
On the iPhone I saved an image to the photo library with modifications (appending data after the image code) using the assest library to save the NSData directly to the photo library. Now I want to read the image back from the photo library as an NSData to read it. If I read it as a UIImage, it changes the data inside the image.
How can I read the photo from the photo library as NSData? I tried looking into the reference URL in 4.1 but no luck.
Edit: I edited to explain why I'm saving it as an NSData. The URL method in the answers works if you just want pure NSData, but does not help in the context that I am asking.
Edit 2: The answer with the getBytes was the answer that did it for me. The code I used was:
int8_t *bytes = malloc([representation size]);
NSUInteger length = [representation getBytes:bytes fromOffset:0 length:[representation size] error:&error];
This was able to get me everything inside the file which gave me the image code PLUS what I added in NSData form.
Edit 3:
Is there a way to do this now in iOS 10 with the PHPhotoLibrary which replaces AssetLibrary? I need the image in NSData being the original RAW image with no modifications.
You should look into the getBytes method of ALAssetRepresentation.
This gives you the original RAW image data of your image and also makes it possible to use a buffer to process the image instead of reading the image into memory all at once.
And of course you can always generate NSData from the getBytes method.
Cheers,
Hendrik
If you already have your ALAsset, you should be able to get an NSData of the default representation using the following:
NSData *data = [NSData dataWithContentsOfURL:[[asset defaultRepresentation] url]];
I'm try to allow users to pull images out of their Photos collections using ALAssetsLibrary. Users can then upload these images. My goal is to allow users to upload any GIFs they may have in their library w/o loosing any animation they may have.
For PNG and JPEG files I can grab the ALAssetRepresentation, use - (CGImageRef)fullResolutionImage to get a CGImageRef, and then save it to NSData using UIImageJPEGRepresentation or UIImagePNGRepresentation.
However, because no similar function exists for GIF files, all I can do is covert the GIF to either JPEG or PNG, but then I lose the animation.
Is there either
a way to grab the NSData straight from an ALAssetRepresentation object or
a way to go from ALAssetRepresentation -> CGImageRef -> NSData without loosing any gif animation frames?
Thanks in advance!
Yes, there is a quite simple way:
Use the getBytes:fromOffset:length:error: method of ALAssetRepresentation. This gives youthe raw file data of the ALAsset, in your case the GIF file.
What's the best way to save and retrieve an array of images across app restarts?
I'm implementing a caching feature for offline viewing of downloaded images and just want to make sure I'm using the right persisting methods.
Thanks!
The quickest & probably best solution would be to persist your images to disk, no question about it.
You could do something like this to save them as JPEG.
NSData *data = UIImageJPEGRepresentation(image, 1.0f);
[data writeToFile:imagePath atomically:YES];