Move Photos to other folder ios - ios

How to move photos from one folder to another folder in iphone?
I have fetched all photos from camera roll. Now, i want some of them to move to some specific folder in iphone. Is it possible to do?

Yes you can, sort of. As in you can access the user media library and you can copy them to you apps sandbox environment. Since iOS has no public filesystem you will not be able to copy/move them directly to some directory.
The images in the user media librarycan accessed with the AssetsLibrary.
You can add new groups to the user media library and add picture to this group. But you can not move, delete or replace existing images in the user media library.

You can Move All files to Under Documents folder or Caches Folder.
You Can Create Folder Programatically in iPhone Photos.
Please check the below Link for Reference:
iphone: copy or move file from document directory folder

here is some (untested) code for copying the thumbnail & full size version of a photo called 'name' from the shared camera roll to the documents directory in your app's sandbox:
NSString *newNameThumb = #"newThumbName.png";
NSString *newName = #"newFullsizeName.png";
NSString *name = #"nameOfSharedPhotoToMove.png";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
// Chooses the photo at the last index
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
if([representation.filename isEqualToString:name]){
UIImage *latestPhoto = [UIImage imageWithCGImage:[alAsset thumbnail]];
NSData *imageData = UIImagePNGRepresentation(latestPhoto);
UIImage *latestFullPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
NSData *imageFullData = UIImagePNGRepresentation(latestFullPhoto);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* newThumbPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:newNameThumb];
[imageData writeToFile:newThumbPath atomically:YES];
NSString* newFullPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:newName];
[imageFullData writeToFile:newFullPath atomically:YES];
*stop = YES; *innerStop = YES;
});
}
}
}];
} failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(#"No photos found or permission denied");
}];

Try this:
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSURL *oldDir = [NSURL fileURLWithPath:#"/tmp/mynewdir"];
NSURL *newDir = [NSURL fileURLWithPath:#"/tmp/mynewdir2"];
[filemgr moveItemAtURL: oldDir toURL: newDir error: nil];
Hope this helps.. :)

Related

NSData WriteToFile Fails while saving a photo from gallery through Share Extension

I am Writing an app which has share extension to save selected photo to my app' local storage from iphone photo gallery.
NSData WriteToFile returns YES but I couldn't find the stored file into the directory in of which I gave path while writing.
So, in short NSData WriteToFile fails to save a photo at given path.
Below is my code.
- (IBAction)acceptButtonTapped:(id)sender
{
__block UIImage *photo;
for (NSExtensionItem *item in self.extensionContext.inputItems)
{
for (NSItemProvider *itemProvider in item.attachments)
{
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage])
{
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
if(image)
{
dispatch_async(dispatch_get_main_queue(), ^{
photo = image;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy_MM_dd_hh_mm_ss"];
NSString *fileName;
fileName = [NSString stringWithFormat:#"%#.jpeg",[formatter stringFromDate:[NSDate date]]];
dataPath = [dataPath stringByAppendingPathComponent:fileName];
NSData * imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];
BOOL isdone = [imageData writeToFile:dataPath atomically:NO];
NSLog(#"%u", isdone);
});
}
}];
break;
}
}
}
[self.extensionContext completeRequestReturningItems:#[] completionHandler:nil];
}
Any Help would be much appreciable.
Thank you.
If you're trying to access the Document directory from the share extension, NO you can't do that. Share extension or other widgets are separate application from their containing app and therefore have their own sandbox. So you will need to use App Groups to share files.
Application groups are primarily targeted for extensions, more specifically, for widgets.
NSFileManager has a method on it containerURLForSecurityApplicationGroupIdentifier: where you can pass in the identifier you created when turning on App Groups for your apps
NSURL *containerURL = [[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:#"group.com.company.app"];
You can save the files to this location, because you can access the shared application groups from both extension and host app.
You're modifying dataPath on each pass through the loop, appending another filename to it. That will create an ever-growing series of badly formed paths that contain all the filenames.
Don't do that. Create a new local variable filePath, and construct a filename into filePath using
filePath = [docsPath stringByAppendingPathComponent: filename];
Log your path and LOOK AT IT. When your program doesn't behave as expected, don't trust any of your assumptions, because one or more of them may be wrong.

Saving gif file created from screenshots to camera roll

I've got a gif in my documents folder I would like users of the app to be able to access and email to use on other platforms that support gif animation.
I generated the gif file using information from this post...
Create and and export an animated gif via iOS?
The gif file generated and animates correctly (opened directly from simulator's documents folder in safari)
Unfortunately when trying to move the file to the camera roll (for easy email by user) using either UIImageWriteToSavedPhotosAlbum or writeImageDataToSavedPhotosAlbum from ALAssetsLibrary the image seems to be converted to a jpg file and loses all animation.
Checked this by emailing file from camera roll and opening on different platform (the functionality I would like users to have).
I have read every post I could find and from what I've seen it seems possible to save gif files directly from a browser to camera roll and even if they do not animate there they do retain that property when opened in another program so I am hoping what I am trying to do is at least possible : )
Thank for any help, have included my gif creation and failed copying attempts below..
- (void) makeGifFile {
////////////////////
NSDictionary *fileProperties = #{
(__bridge id)kCGImagePropertyGIFDictionary: #{
(__bridge id)kCGImagePropertyGIFLoopCount: #0, // 0 means loop forever
}
};
///////////////////
NSDictionary *frameProperties = #{
(__bridge id)kCGImagePropertyGIFDictionary: #{
(__bridge id)kCGImagePropertyGIFDelayTime: #0.06f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
}
};
///////////////////////
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:#"animated.gif"];
////////////////////////
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, self.screenshotnumber, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
/////////////////////////////////
for (NSUInteger i = 1; i < self.screenshotnumber+1; i++) {
#autoreleasepool {
////
NSString *name = [NSString stringWithFormat: #"Screenshot%d", i];
NSString *pathstring = [NSString stringWithFormat: #"Documents/%#.png", name];
NSString *gifPath = [NSHomeDirectory() stringByAppendingPathComponent:pathstring];
/////
UIImage *image = [UIImage imageWithContentsOfFile:gifPath];
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); }
}
///////////////////////////////////////
if (!CGImageDestinationFinalize(destination)) {
NSLog(#"failed to finalize image destination");
}
CFRelease(destination);
NSLog(#"url=%#", fileURL);
//////////////////////////////
///
/// saved in documents directory
/////////////////////////////
//////////////////////////
//////
///// now move to camera roll
///////////////////////
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *gifImagePath = [NSString stringWithFormat:#"%#/%#", documentDirectory, #"animated.gif"];
UIImage *gifImage = [UIImage imageWithContentsOfFile:gifImagePath];
UIImageWriteToSavedPhotosAlbum(gifImage, nil, nil, nil);
CCLOG(#"wrote to camera roll");
//////////////////////// gets saved as JPG not gif
//////// next try...
NSData *data = [NSData dataWithContentsOfFile:gifImagePath]; // Your GIF file path which you might have saved in NSDocumentDir or NSTempDir
[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(#"Error Saving GIF to Photo Album: %#", error);
} else {
// TODO: success handling
NSLog(#"GIF Saved to %#", assetURL);
// success(gifImagePath);
}
}];
/////////// also gets saved as jpg
}
My methods for creating screenshots for those interested... I have lost track of the post I found this on... if anyone can provide me with the link I will give due credit here ...
Included to have all relevant functions together in case it helps anyone else :)
-(UIImage*) screenshotWithStartNode:(CCNode*)startNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize viewSize = [[CCDirector sharedDirector] viewSize];
CCRenderTexture* rtx =
[CCRenderTexture renderTextureWithWidth:viewSize.width
height:viewSize.height];
[rtx begin];
[startNode visit];
[rtx end];
return [rtx getUIImage];
}
- (void) saveScreenShotWithName: (NSString*) name
{
CCScene *scene = [[CCDirector sharedDirector] runningScene];
CCNode *n = [scene.children objectAtIndex:0];
UIImage *tempimage = [self screenshotWithStartNode:n];
NSString *pathstring = [NSString stringWithFormat: #"Documents/%#.png", name];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:pathstring];
// Write image to PNG
[UIImagePNGRepresentation(tempimage) writeToFile:savePath atomically:YES];
}
Simple loop creates files and then another deletes files from documents directory after gif creation
Sadly this can't be solved. The reason for this is that Photos app can't (at the present) display animated GIFs and it only displays one frame from the GIF as a static image. That doesn't mean though that the gif wasn't saved properly. I haven't tried your code but everything seems Ok.
There's a way of testing this. In Messages app (for example) GIFs are being played correctly, so if you share the GIF image from Photos app via ActivityController, select Message and then send it to yourself, you should see animated GIF image in Messages app.

UI Activity View Errors

I'm quite the beginner to iOS Programming, and I googled how to get the UI Activity View implemented, but I'm getting errors that I do not quite understand. Anyone that can help me figure out what the errors mean in more specific detail and how to fix them, it would be greatly appreciated.
#pragma mark - SHARING OPTIONS (using a DocumentInteractionController) =============
/* =================
NOTE: The following methods work only on real device, not iOS Simulator, and you should have apps like Instagram, iPhoto, etc. already installed into your device!
================= */
-(void)shareImageToAllAppsAvailable {
NSLog(#"This code works only on device. Please test it on iPhone!");
// makes an NSURL file to the processed Image that needs to be saved
NSURL *fileURL;
docIntController.delegate = self;
//Saves the Image to default device directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"My Selfie.jpg"];
UIImage *image = combinedImage;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
//Load the Image Path
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"My Selfie.jpg"];
// this blank line here creates error 'use of undeclared identifier 'showActivityViewController''
// Create the URL path to the Image to be saved
fileURL = [[NSURL alloc] initFileURLWithPath:getImagePath];
// Open the Document Interaction controller for Sharing options
-(void)showActivityViewController
{
//-- set up the data objects
NSString *textObject = _aTextView.text;
UIImage *image = [UIImage imageNamed:#"My Selfie.jpg"];
NSArray *activityItems = [NSArray arrayWithObjects:textObject, url, image, nil];
//-- initialising the activity view controller
UIActivityViewController *avc = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
//-- define the activity view completion handler
avc.completionHandler = ^(NSString *activityType, BOOL completed){
NSLog(#"Activity Type selected: %#", activityType);
if (completed) {
NSLog(#"Selected activity was performed.");
} else {
if (activityType == NULL) {
NSLog(#"User dismissed the view controller without making a selection.");
} else {
NSLog(#"Activity was not performed.");
}
}
};
}
You're missing a closing } before your -(void)showActivityViewController declaration.
It looks like you're trying to call that method by defining it within another method, which is not valid Objective-C. Use the self construct to reference methods defined in the same class.
#pragma mark - SHARING OPTIONS (using a DocumentInteractionController) =============
/* =================
NOTE: The following methods work only on real device, not iOS Simulator, and you should have apps like Instagram, iPhoto, etc. already installed into your device!
================= */
-(void)shareImageToAllAppsAvailable {
NSLog(#"This code works only on device. Please test it on iPhone!");
// makes an NSURL file to the processed Image that needs to be saved
NSURL *fileURL;
docIntController.delegate = self;
//Saves the Image to default device directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"My Selfie.jpg"];
UIImage *image = combinedImage;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
//Load the Image Path
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"My Selfie.jpg"];
// this blank line here creates error 'use of undeclared identifier 'showActivityViewController''
// Create the URL path to the Image to be saved
fileURL = [[NSURL alloc] initFileURLWithPath:getImagePath];
// Open the Document Interaction controller for Sharing options
[self showActivityViewController]; //added
} //added
-(void)showActivityViewController
{
//-- set up the data objects
NSString *textObject = _aTextView.text;
UIImage *image = [UIImage imageNamed:#"My Selfie.jpg"];
NSArray *activityItems = [NSArray arrayWithObjects:textObject, url, image, nil];
//-- initialising the activity view controller
UIActivityViewController *avc = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
//-- define the activity view completion handler
avc.completionHandler = ^(NSString *activityType, BOOL completed){
NSLog(#"Activity Type selected: %#", activityType);
if (completed) {
NSLog(#"Selected activity was performed.");
} else {
if (activityType == NULL) {
NSLog(#"User dismissed the view controller without making a selection.");
} else {
NSLog(#"Activity was not performed.");
}
}
};
}

ALAssetLibrary for audio

I am trying to load all the files(audio, video and images) in my application using ALAssetLibrary by this piece of code:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate everything.
[group setAssetsFilter:[ALAssetsFilter allAssets]];
// Accessing all the assets.
for (int i = 0;i<[group numberOfAssets] ; i++) {
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
NSURL *url = [representation url];
NSLog(#"%#",url);
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
//Doing some stuff with assets
}
}];
}
}
failureBlock: ^(NSError *error) {
NSLog(#"No groups");
}];
However, this only loads video and images, not the audio files. How can I do that?
Regarding Apple.Developer ALAsset documentaion ,An ALAsset object represents a photo or a video managed by the Photo application.
Assets can have multiple representations, for example a photo which was captured in RAW and JPG. Different representations of the same asset may have different dimensions.
So i think ,ALAssetsLibrary will not retrieve what you need :)

Proper way of saving and loading pictures

I am making a small app where the user can create a game profile, input some data and a picture that can be taken with the camera.
I save most of that profile data with the help of NSUserDefaults, but a friend discouraged me from saving the profile image in NSUserDefault.
What's the proper way to save and retrieve images locally in my app?
You should save it in Documents or Cache folder. Here is how to do it.
Saving into Documents folder:
NSString* path = [NSHomeDirectory() stringByAppendingString:#"/Documents/myImage.png"];
BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path
contents:nil attributes:nil];
if (!ok)
{
NSLog(#"Error creating file %#", path);
}
else
{
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
[myFileHandle writeData:UIImagePNGRepresentation(yourImage)];
[myFileHandle closeFile];
}
Loading from Documents folder:
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
You can also use UIImageJPEGRepresentation to save your UIImage as a JPEG file. What's more if you want to save it in Cache directory, use:
[NSHomeDirectory() stringByAppendingString:#"/Library/Caches/"]
One way to do this is use the application's document directory. This is specific to a application and will not be visible to other applications.
How to create this:
Just add a static function to App Delegate and use the function where ever the path is required.
- (NSString )applicationDocumentDirectory {
/
Returns the path to the application's documents directory.
*/
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
Hope it Helped..
I think this("iphone-user-defaults-and-uiimages") post addresses your issue. Don't save blobs to a property list such as NSUserDefaults. In your case I would write to disk directly instead.

Resources