I am using UIImageWriteToSavedPhotosAlbum to store a new photo on the iPhone (code see below).
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
This works fine. However I would like to store new photos in a different album. Not the default one.
EDIT
I am using the following block to create the album.
ALAssetsLibraryGroupResultBlock resultblock = ^(ALAssetsGroup *assetGroup)
{
[assetGroup addAsset:myasset]
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"in failureblock, got an error: %#",[myerror localizedDescription]);
};
[library addAssetsGroupAlbumWithName:name resultBlock:resultblock failureBlock:failureblock];`
I am using the UIImagePickerController. How do I get the asset of a new image? When/where do I call UIImageWriteToSavedPhotosAlbum to store it?
Since iOS 5 you can create a new album with addAssetsGroupAlbumWithName:resultBlock:failureBlock: using the ALAssetsLibrary
Here's a link to a detailed tutorial on how to do that — http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
Related
I've using uiimagepickercontroller and taken images via camera and stored into folder that was created in sandbox. That stored images are not displayed in iPads or iPhone photo gallery. I want to display those sandbox pictures in device gallery. Any one help me out from this.
My optimum goal is to copy image from sandbox and paste it to device's internal memory.
You use the UIImageWriteToSavedPhotosAlbum() function.
UIImageWriteToSavedPhotosAlbum(IMAGE_TO_STORED, nil, nil, nil);
Edit:
- (IBAction)savedPhoto:(id)sender{
UIImageWriteToSavedPhotosAlbum(IMAGE_TO_STORED, nil, nil, nil);
}
Another way to stored image is:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library saveImage:image toAlbum:nil withCompletionBlock:^(NSError *error) {
if (error!=nil)
{
NSLog(#"Error: %#", [error description]);
}
}];
In iOS 10 I start to replace asset-library to PhotoKit to manage image picker. But there is an issue.
The specific step is that app needs system camera and user shot a photo after that the delegate method imagePickerController:didFinishPickingMediaWithInfo: will be called.
Then, here is my code:
UIImage *pickerImage = [[info objectForKey:UIImagePickerControllerOriginalImage] fixOrientation];
PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
__block NSString *localId;
[photoLibrary performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:pickerImage];
localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
// to get localIdentifier and reload collectionView
}
}];
line 1 that fixOrientation is a custom category that return UIImage instance.
Only call [PHAssetChangeRequest creationRequestForAssetFromImage:] method performs fine. However, when I want to fetch newly created photo to use [assetChangeRequest placeholderForCreatedAsset] , it shows memory leak and app crash.
Above all, any solution to fetch created photo just moment or other methods to solve that by using Photos Framework?
I do not know much about Objective-C.
I need to save the image in the gallery to custom name album and i used Canvas2ImagePlugin but image save to album camera roll.
Canvas2ImagePlugin using:
UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
How to use the name album in the function UIImageWriteToSavedPhotosAlbum? Or use another function?
Actually i don't know about phonegap. but i can give you hint that how we can achieved in ios. with help of ALAssetsLibrary, we can stored photo to custom album.
[self.library saveImage:image toAlbum:#"Touch Code Magazine" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
Hey I'm new to iPhone and I have been trying to make an gallery kind of app. Basically, what I want to do is that i need to save all the captured images into a specific folder like a new album "My_App Images" related to our app name in iPhone device gallery, it's working for me, but I am having trouble to change the image file name, i don't know that Is it possible to specify a file name? Using iPhoto, currently i am getting image file name as "IMG_0094.jpg", can we change it with any other file name like "Anyfilename.png" format programmatically?
here is my code for saving images to the specific album :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[self.library saveImage:image toAlbum:#"My_App Images" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Image saving error: %#", [error description]);
}
}];
[picker dismissViewControllerAnimated:NO completion:nil];
}
Any source or link for reference is appreciated. Thanks for the help!
There is a way to kinda do that, by setting the image IPTC metadata field "Object Name". If you later import the image to iPhoto, then this name will be used as its title.
See details (and code) at http://ootips.org/yonat/how-to-set-the-image-name-when-saving-to-the-camera-roll/ .
Do you meant,
// Build NSData in memory from the btnImage...
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
// Save to the default Apple (Camera Roll) folder.
[imageData writeToFile:#"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO];
Now adjust the path of folder as per your folder name...
Sorry to disappoint you, but it seems that you can not change the name of the photos, before or after saving, in the photo album, custom or not. Here is a post to explain it:
iOS rename/delete albums of photos
Edit
So, to clarify my comment, use the following override:
Download the NSMutableDictionary category for metadata of image here.
Also download the sample project CustomAlbumDemo from here and modify the NSMutableDictionary+ImageMetadata.m file in the CustomAlbumDemo project as:
-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
//write the image data to the assets library (camera roll)
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
[metadata setDescription:#"This is my special image"];
[self writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}];
}
How can I save the images to iPad's Gallery, and again read them from there into my App.
Is it possible to create a folder structure in iPad's gallery where I could store images generated through my app.
Actually I am able to save the images to gallery, I am using this
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
But don't know how to load it back into app from gallery. ?
Use the ALAssetsLibrary, then you can call writeImageToSavedPhotosAlbum:metadata:completionBlock: to save the image and in the completion block you get the assetURL. This can later be used to get the image back from the library.
Docs are here .
To get the image for the asset url:
[self.assetsLibrary assetForURL:imageURL resultBlock:^(ALAsset *asset) {
if (asset != nil) {
imageView.image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
}
} failureBlock:^(NSError * error) {
NSLog (#"Error getting image asset: %#", error);
}];