Why is it so hard to upload a photo with Location data in its exif to the server? I am breaking my head not being able to solve this. Whenever I am sending the photo to the server all of its location information is being stripped off from the photo.
I have tried getting UIImage from the UIImagePickerController using both
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
and also UIImagePickerControllerOriginalImage.
Kindly someone help me
To preserve the exif info, you need to use the raw data, not just the UIImage. You can get it from the ALAsset's defaultRepresentation, something like this:
ALAssetRepresentation* representation = [myAsset defaultRepresentation];
int size = representation.size;
NSMutableData* data = [[NSMutableData alloc]initWithCapacity:size];
void* buffer = [data mutableBytes];
[representation getBytes:buffer fromOffset:0 length:size error:nil];
data = [NSMutableData dataWithBytes:buffer length:size];
I'm not near xcode to test it right now, but it should work.
You can use CLLocation to get the current location.
At the time of image upload to server you need to need to get the current location through CLLocationManager and update this location parameters to server with the captured image.
Please check the reference link for location update and how to upload photos
Related
let image_data = UIImageJPEGRepresentation(self.imagetoadd.image!,0.0)
The image in ios, am using swift 3 to do this is being uploaded rotated.How can I solve such thing?
JPEG images usually contain an EXIF dictionary, here are stored a lot information about how the image was taken, image rotation is one of it.
UIImage instances keeps these information (if the original image has it) as well inside a specific property called imageOrientation.
As far as I remember this information is ripped of by using the method UIImageJPEGRepresentation.
To create a correct data instance with the above information you must use Core Graphics methods, or normalize the rotation before sending the image.
To normalize the image something like that should be enough:
CGImageRef cgRef = imageToSave.CGImage;
UIImage * fixImage = [[UIImage alloc] initWithCGImage:cgRef scale:imageToSave.scale orientation:UIImageOrientationUp];
To keep the rotation information:
CFURLRef url = (__bridge_retained CFURLRef)[NSURL fileURLWithPath:path];//Save data path
NSDictionary * metadataDictionary = [self imageMetadataForPath:pathToOriginalImage];
CFMutableDictionaryRef metadataImage = (__bridge_retained CFMutableDictionaryRef) metadata;
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL);
CGImageDestinationAddImage(destination, image, metadataImage);
if (!CGImageDestinationFinalize(destination)) {
DLog(#"Failed to write image to %#", path);
}
Where the -imageMetadataForPath:
- (NSDictionary*) imageMetadataForPath:(NSString*) imagePath{
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];
CGImageSourceRef mySourceRef = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, NULL);
NSDictionary * dict = (NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(mySourceRef,0,NULL));
CFRelease(mySourceRef);
return dict;
}
This is a copy and paste from a project of mine, you probably need to do a huge refactoring, also because it is using manual memory management in core foundation and you are using SWIFT. Of course by using this last set of instructions, the backend code must be prepared to deal with image orientation too.
If you want to know more about rotation, here is a link.
I have a question on how to convert an image with exif data to NSData type. I get the image rather capture an image in camera or get the image from camera roll.
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
After picking the image, I went here to convert the image with exif to NSData and sent to the sever database. However, I tried many approach but it fails. When I get the picture file in database, it lost all the exit and metadata information of the picture.
So, I want to ask, is there any way to convert an image and keep the exif and metadata in NSData Type?
Thanks all.
You can do something like that, but if you don't need the NSData for further operation I strongly suggest you to save the image on disk:
NSDictionary *metadata = [info objectForKey: UIImagePickerControllerMediaMetadata];
CFURLRef url = (__bridge_retained CFURLRef)[NSURL fileURLWithPath:path];
CFMutableDictionaryRef metadataImage = (__bridge_retained CFMutableDictionaryRef) metadata;
NSMutableData * destData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)destData,kUTTypeJPEG,1,NULL);
CGImageDestinationAddImage(destination, uiImage.CGImage, metadataImage);
if (!CGImageDestinationFinalize(destination)) {
DLog(#"Failed to write image to %#", path);
}
else {
DLog(#"Writing image to %#", path);
}
I'm trying to display a BLOB image (get from web server using Json) in my iOS app, but when I run my application I get an empty UIimageView, here is my code :
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:encodedUrl]];
NSData *profileImage1 = [[NSData alloc] initWithBytes:[dataURL bytes] length:[dataURL length]];
UIImage *profileImage2 = [UIImage imageWithData:profileImage1];
[profilImage setImage:profileImage2];
How can I fix this problem?
[UIImage imageWithData:data] only parses known image file formats like JPEG, PNG, etc. (Full info in the decomentation). Passing blobs isn't supported by UIImage. You need to do some decoding to be able to use the data for the UIImage. You can use GMTBase.64 for encoding and decoding of data. Read the docs and other posts and you'll find out how to change your code.
Hope this helps.
Im trying to attach a Google Maps image to an email generated in my app, however Google Maps does not return a specific image from its url. If I search a url with .png etc at the end of it, I can get the image fine, but how can I get the image from a site that doesn't have that,
ie:
http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap\\&markers=size:mid%%7Ccolor:red%%7CNew+York&sensor=false
Im using NSData dataWithContentsOfURL: which then attaches to my email.
I have also tried to open the url in an iframe within the email with the same result, works with a .png url, not maps.
Any help would be greatly appreciated, thanks.
Try this,
NSString *urlString = #"http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap\\&markers=size:mid%%7Ccolor:red%%7CNew+York&sensor=false";
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSData *myData = [NSData dataWithContentsOfURL:webURL];
UIImage *image = [[UIImage alloc] initWithData:myData];
self.imageView.image = image;
http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap%%5C&markers=size:mid%%257Ccolor:red%%7CNew+York&sensor=false
Just needed to edit some of the % \ etc to incorporate the UTF8 encoding, however if you use the encoding method karthika suggested, it breaks the search. All I did was hard code the string and removed the added encoding before the address and its working perfectly.
I am caching an NSData object containing image data retrieved from the web. The image displays correctly before caching. When I retrieve the data object from the cache, the data can no longer be used to create a UIImage, even though the data objects are identical.
Please see the relevant snippets of my code below
NSData *webData= [NSData dataWithContentsOfURL:webPath]; //retrieve from web
UIImage *webImage = [UIImage imageWithData:webData]; //works fine
[webData writeToURL:filePath atomically:YES]; //cache
NSData *cacheData = [NSData dataWithContentsOfURL:filePath]; //get from cache
if ([cacheData isEqualToData:webData]) NSLog(#"Equal"); //Data objects are equal
UIImage *cacheImage = [UIImage imageWithData:cacheData]; //cacheImage is nil
I can fix the problem by changing the way I store my data to the cache
NSData *temp = UIImageJPEGRepresentation(webImage, 1.0):
[temp writeToURL:filePath atomically:YES];
Now the webData and cacheData are no longer equal, but cacheImage is not nil and displays properly.
EDIT - After a bit more testing, I realized I get the same problem using UIImageJPEGRepresentation as well.
Anyone know why this would be?
Thanks.
Figure out the problem was that I was trying to do all this before my view controller was fully loaded.