I am trying to write an iPhone app that will allow me to upload a jpg via multi-part form.
Sometimes, the uploaded photo looks corrupted on the server with weird pixels and the photo looks cropped.
What could be the reason for that?
How come I don't get a http error response from the server? (upload backend is written in PHP)
You can use base64 encoding like shown below
UIGraphicsBeginImageContext(CGSizeMake(145.0, 145.0));
[takephoto.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *LargeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(LargeImage, 0.25);
NSString *strImageFinalBASE64 =[data base64Encoding];
takePhoto can be your UIImageView:
IBOutlet UIImageView *takephoto;
Related
I am making a small application where I am receiving images from web-service. I am getting the image url. And the format of images is different different like .jpg,.gif,.png etc. And I want to save these images into my sandbox and later on show it into image view.
To do this I am giving a self name and gave the extension to .png. But at the time of retrieving the .png images are shown but different one's are not shown. So can you give me any idea how to convert all images to .png format or any other approach to do this?
after you retrieve your images/url save them this way:
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
Would it not be easier if you use an image library like SDWebImage which handles all the asynchronous image downloading and caching of the image onto your device?
https://github.com/rs/SDWebImage
Then you can do something like:
[myImageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
I'm writing a function to download a regular PNG file from web server and save it to iPhone Photos album. I don't get any error for my code listed below but UIImagePNGRepresentation doesn't perform the convert at all. All images I got were black. I have to enable "auto-enhanced" under Photos to display those image properly. Any clue what's wrong with my code? Thanks in advance.
UIImage *orgImg = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://server.com/test.png"]]];
NSData * convertedData = UIImagePNGRepresentation(orgImg);
UIImage * img = [UIImage imageWithData: convertedData];
// Save Image to Photo Album
UIImageWriteToSavedPhotosAlbum(img, self,
#selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), nil);
Some more details of these two output images, orgImg and img:
If I pass orgImg to UIImageWriteToSavedPhotosAlbum directly, I did get the same PNG image hosted on the server. However Photos application on my iPhone displays it as a black image because Photos doesn't enhance PNG format automatically.
As for the img I got after using UIImagePNGRepresentation, the image data has been changed by the method, some meta data such as width/height have been removed from the original image. However Photos app on my iPhone still display it as an all black image.
Both images display properly if I copy them into either Safari or email. I didn't get any problem if I use JPEG in the server side either. Just curious why only PNG format doesn't work.
How does one load or fetch an image that was saved to disk?
I wrote the image to the documents filepath using NSData and a PNG representation. How does one get this image back? UIImage.
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];
As ever, see the UIImage documentation at Apple. It is your friend.
We have images stored in our SQL Server db in an image type field. Im just wanting to know how I go about getting them into a UIImageView in my iPad app. My iPad app talks to the SQL Server over an OData service provider so the image field comes across as type NSData so I tried the following code already:
UIImage *currentImage = [[UIImage alloc] initWithData:[currentEntityImage getEntityImageData]];
[myImageView setImage:currentImage];
Where "[currentEntityImage getEntityImageData]" is the NSData field converted from the image field. The problem is I can never get anything to display in the UIImageView.
Here is the corresponding code I use in our .net application that shows the image correctly:
Dim EntImage As EntityImage = CType(e.Entities.Item(0), EntityImage)
Dim ms As New System.IO.MemoryStream(EntImage.EntityImage)
Me.imgEntityImage.Image = System.Drawing.Image.FromStream(ms)
This works fine and shows the image correctly (i am accessing the same data on both platforms).
Any ideas on what im missing in my obj-c code? Ive been pulling my hair out all morning on this.
Thanks in advance
maybe the data returned from OData is encoded Base64.
Try decoding it with http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
Hope it helps
Try this
NSData *imageData =[NSData dataWithData:[currentEntityImage getEntityImageData]];
UIImage *animalImage = [[UIImage alloc] initWithData:imageData];
[myImageView setImage:animalImage];
I'm trying to take a screen shot of my app's current view and save it to photo album (to then be emailed or MMS'ed).
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, #selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
This works but the resulting image apears to be larger (533x800px) and heavily compressed when I email it from the photo library.
I've tried first writing the UIImage to file and then saving to album but still get the same issue.
If I used the in-built screenshot functionality on the iPhone the view saves correctly to photo album at 320x480 but the above code appears to save a larger image for some reason?
Thanks!
I found a decent workaround, which is to essentially rewrap the UIImage as a PNG, then save the rewrapped version. Code looks something like this:
UIImage* im = [UIImage imageWithCGImage:myCGRef]; // make image from CGRef
NSData* imdata = UIImagePNGRepresentation ( im ); // get PNG representation
UIImage* im2 = [UIImage imageWithData:imdata]; // wrap UIImage around PNG representation
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil); // save to photo album
I had that same error, on my part, that was solved when I rounded the decimal points to be the same scale as the iPhone, try it, make sure the scale is 1.0, 2.0, etc and not 3.1, that will throw it off.