I tried to write the file format of "output.aif". I used the below code to write it to a path. The file got saved in two locations one at the path I specified and one more it got saved in the image gallery by default and the name of the file is of a random number. While exiting the application I use to delete the saved ".aif" files from the location I saved, But I couldnt delete the files that got saved in the gallery as I dont know the name of the files.
Is there any way to stop saving the "aif" files from gallery which is happening default.
Can any one suggest a different code to save the file in a location, which wont save the file in gallery?
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
}
else{
NSString *outputTp = [[[NSHomeDirectory() stringByAppendingString:#"/Documents/"] stringByAppendingString:#"output.aif"] retain];
inUrl = [url retain];
outUrl = [[NSURL fileURLWithPath:outputSound] retain];
reader = [[EAFRead alloc] init];
writer = [[EAFWrite alloc] init];
self.url = outputTp;
// this thread does the processing
[NSThread detachNewThreadSelector:#selector(processThread:) toTarget:self withObject:nil];
}
}];
Related
I need to use the iOS device camera to take a photo and then upload it to the server. The problem is that, by default, the UIImage taken form UIImagePickerController info does not include GPS metadata.
Following this post I have been able to save the image in the Camerra Roll with the correct GPS info. The code used is the following one:
- (void)saveImage:(UIImage *)imageToSave withInfo:(NSDictionary *)info {
// Get the image metadata (EXIF & TIFF)
NSMutableDictionary * imgMtd = [[info objectForKey:UIImagePickerControllerMediaMetadata] mutableCopy];
// create CLLocation for image
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
CLLocation * loc = [locationManager location];
if (loc) {
[imgMtd setObject:[self gpsDictionaryForLocation:loc] forKey:(NSString*)kCGImagePropertyGPSDictionary];
}
// Get the assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// create a completion block for when we process the image
ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
^(NSURL *newURL, NSError *error) {
if (error) {
NSLog( #"Error writing image with metadata to Photo Library: %#", error );
} else {
NSLog( #"Wrote image %# with metadata %# to Photo Library",newURL,imgMtd);
}
};
// Save the new image to the Camera Roll, using the completion block defined just above
[library writeImageToSavedPhotosAlbum:[imageToSave CGImage] metadata:imgMtd completionBlock:imageWriteCompletionBlock];
}
However, I am using this photo to upload it to a server. Therefore, the desired behavior is to save it at a temporal path (so the image won't be available at the Camera Roll), upload it to the server and then delete it. With the code provided, I am making the image public, and don't know its path to upload it.
I try to compress a jpg with 0.4 quality. I printed the data size using NSlog, it shows the NSData size is 337613, but after save to album, the file was increased to 677947.
What can I do with this?
PS: image object is from Camera.
NSData *newData =UIImageJPEGRepresentation(image,0.3f);
UIImage *compressdImg30 = [UIImage imageWithData:newData];
NSLog(#"[after 0]newData.length=%u",[newData length]); // print 337613 in debug
UIImageWriteToSavedPhotosAlbum(compressdImg30,nil,nil,nil);
then I plug iPhone and copy the file to my Mac Mini, and type "ls -ltr" to see the file size, then showing 677947.
You can use ALAssetsLibrary's
-writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
method to save image data to library.
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Completion block/Do check the error if any");
}];
Try it and see
I downloaded an gif image from the network using AFNetworking 2.0 then save it to camera roll using ALAssetsLibrary
[assetsLibrary writeImageToSavedPhotosAlbum:[responseObject CGImage] orientation:(ALAssetOrientation)[responseObject imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
[App showAlertWithTitle:#"Error" message:#"Save message failed"];
}
else
{
[App showAlertWithTitle:#"Success" message:#"Saved success"];
}
}];
Then I tried to retrieve this image from camera using UIImagePickerViewController, but the image I retrieved was not a GIF image but a jpeg image with reference url:
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=2E7C87E4-5853-4946-B86B-CC8AAF094307&ext=JPG";
I don't know whether the fault is ALAssetsLibrary or UIImagePickerViewController and how to surpass it
The photo library does not support GIFs.
It has support for PHAssetMediaTypeImage (a JPG), PHAssetMediaTypeVideo (a MOV), or PHAssetMediaTypeAudio (probably an M4A, not sure here).
https://developer.apple.com/library/ios/documentation/Photos/Reference/Photos_Constants/index.html#//apple_ref/c/tdef/PHAssetMediaSubtype
The writeImageToSavedPhotosAlbum: methods only save still images as JPEGs, as do the new Photos methods. However, there are ways of saving other formats, including (yes!) GIF.
You don't need to mess about with CGImageRefs—just grab the GIF data and then save it, using the writeImageDataToSavedPhotosAlbum:metadata:completionBlock: method. Something like this:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSData *data = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://somewhere/something.gif"]]];
[library writeImageDataToSavedPhotosAlbum:data
metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
[App showAlertWithTitle:#"Error" message:#"Save message failed"];
} else {
[App showAlertWithTitle:#"Success" message:#"Saved success"];
}
}];
See this answer.
If you want to generate a GIF, it's somewhat more complex, but simply saving one is straightforward.
Thanks for reading. I've created a GIF using methods from this question:
Create and and export an animated gif via iOS?
I'm trying to use the only method that appears to be able to save non JPG/PNG images to the camera roll, ALAssetLibrary's writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
I save the Gif to the temp Directory like this:
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:#"/animated.gif"];
NSURL *fileURL = [NSURL fileURLWithPath:exportPath isDirectory:NO];
Then access the NSData like:
NSData * gifData = [NSData dataWithContentsOfFile:fileURL.absoluteString];
The GIF is created as I'm able to display it in a UIImageView, but when I try to save it, the method returns as a success (no error) but doesn't actually save (returns Nil for the NSURL * assetURL and does not appear in the camera roll).
How can I get my GIFs to save successfully to the camera roll?
**
Solution 01 : Only saving the existing GIF file to Camera Roll
**
As I understand your problem. You are able to generate a GIF file but cannot save and also view it to the Camera Roll.
So I am attaching a sample test using existing GIF File.
Step 01. I copied a gif IMG_0009.GIF file in my Application Document directory.
Step 02 Than I use the below code to load this files NSData:
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:#"IMG_0009.gif"];
NSData *gifData = [NSData dataWithContentsOfFile:[fileURL path]];
Step 03: Now I save the file in the Media Directory:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:gifData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Success at %#", [assetURL path] );
}];
The Asset URL is proper. Now you can check you media directory. you can locate the saved gif image.
Have Fun :)
**
Solution 02: Demo of Creating and saving GIF to Camera roll
**
I cloned some solution to show creating and saving of GIF files to Camera Roll.
You can download and check my fork at github:
The demo creates a GIF file by taking 2 or more images and save in the Camera Roll Directory
https://github.com/bllakjakk/Giraffe
The main Code to focus is like below:
[export encodeToFile:tempFile callback:^(NSString * aFile) {
NSLog(#"Path: %#", aFile);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSData *data = [NSData dataWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:aFile]];
[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Success at %#", [assetURL path] );
}];
}];
It uses the library as I mentioned in my solution before http://jitsik.com/wordpress/?p=208
How to verify:
Step 01: Run the demo project.
Step 02: As directed by the application add 2 images and click Export.
Step 03: Now check the camera roll you will find the created gif.
Previous:
GIF is a proprietary format, so you would need a 3rd party lib to save it.
check following link: http://jitsik.com/wordpress/?p=208
I found the issue was that I was unable to actually grab the GIF from the file. I switched from using CGImageDestinationCreateWithURL to CGImageDestinationCreateWithData and used a CFMutableDataRef to hold the Gif data. I don't know why, but that made saving to camera roll with writeImageDataToSavedPhotosAlbum work.
Has this been updated to work with iOS 9, and the deprecation of ALAssets? I do not see similar calls in PHPhotoLibrary.
Here is an updated answer using PHPhotoLibrary, since ALAssetsLibrary is deprecated.
I used this answer from another user - 陈星旺
PHPhotoLibrary save a gif data
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:#"/animated.gif"];
NSURL *fileURL = [NSURL fileURLWithPath:exportPath isDirectory:NO];
NSData * gifData = [NSData dataWithContentsOfFile:fileURL.absoluteString];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
[[PHAssetCreationRequest creationRequestForAsset]
addResourceWithType:PHAssetResourceTypePhoto
data:gifData
options:options];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(#"image saved!");
} else {
NSLog(#"error saving image - %#", error ? error.localizedDescription : #"");
}
}];
If you needed to download the GIF data from a URL, you could use this:
NSData *gifData = [NSData dataWithContentsOfURL:theGIFsURL];
The current best solution:
https://stackoverflow.com/a/21888830/1786820
I am trying to do one better, by opening the Instagram app with a preselected video file from the PhotoRoll, and a preloaded caption. In the Flipagram app, they do just this. When you hit share on a video, they save it your camera roll, suggest a caption, then direct to the Instagram app photo selection screen, with the video preselected. Even if the video is not the latest media in the PhotoRoll, it correctly highlights the correct video, along the caption prepared in the Flipagram app.
Is this possibly an undocumented iPhone hook?
Any help is appreciated.
I came up with the idea to allow my app to accept the instagram:// URL schema. The hook from Flipagram opened up in my app as the following:
instagram://library?AssetPath=assets-library%3A%2F%2Fasset%2Fasset.mp4%3Fid%3D8864C466-A45C-4C48-B76F-E3C421711E9D%26ext%3Dmp4&InstagramCaption=Some%20Preloaded%20Caption
The undocumented iPhone hook that allows you to automatically select assets from the iPhones photo roll, and preload a caption for the video. This should give you the same user experience that Flipagrams app has with sharing a video to Instagram.
NSURL *videoFilePath = ...; // Your local path to the video
NSString *caption = #"Some Preloaded Caption";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoFilePath] completionBlock:^(NSURL *assetURL, NSError *error) {
NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:#"instagram://library?AssetPath=%#&InstagramCaption=%#",[assetURL absoluteString].percentEscape,caption.percentEscape]];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
}];
Works great!
Update:
Instagram has removed the ability to pass the caption to their app. The best solution now it to just copy the desired caption to the paste board.
The answer is that it is not pulling the video from the camera roll at all, it might just look like it is.
Documentation here: http://instagram.com/developer/mobile-sharing/iphone-hooks/
The relevant bit is the bottom section "Document Interaction".
You would do this by doing something like this:
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"instagram.igo"];
NSData *data = // set this yourself
NSError *error = nil;
if (! [data writeToFile:filePath options:NSDataWritingAtomic error:&error])
{
// error here
}
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.documentInteractionController.delegate = self;
self.documentInteractionController.UTI = #"com.instagram.exclusivegram";
self.documentInteractionController.annotation = #{ #"InstagramCaption" : #"caption text here" };
const BOOL couldOpen = [self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:myView animated:YES];
Set the data, the caption, and the view to present from yourself. Notice the UIDocumentInteractionController is also a property. It should be retained somewhere and not just a local variable in a method because it needs to exist outside of that scope when the method completes.