I am using the following code to save image into photo album,
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[my_Image CGImage] orientation:(ALAssetOrientation)[my_Image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
Failure
} else {
Success
}
}];
[library release];
The above code is working fine in all the iPad other than iPad mini.
Actually i dont have iPad mini device. But my client saying that issue. I unable to find the error what actually happened in iPad mini. So how to fix this issue. Thanks.
Add check for ALAuthorizationStatus.
If ALAuthorizationStatus is ALAuthorizationStatusRestricted or ALAuthorizationStatusDenied then your image will not get stored in Photo album.
To check ALAuthorizationStatus. Use following:
ALAuthorizationStatus authorize = [ALAssetsLibrary authorizationStatus];
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]);
}
}];
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'm using Apple's AVCam source code to create a custom camera, its working like a charm, the problem is once I captured a video or image with it, and then checked it into photo library its orientation gets changed to landscape (even I captured it in portrait orientation). I searched a lot for this, but couldn't find a way for this. Any help?
For a note, my app only supports portrait and capturing should only in portrait.
Update:
AVCaptureConnection *captureConnection = ...
if ([captureConnection isVideoOrientationSupported])
{
AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationPortrait;
[captureConnection setVideoOrientation:orientation];
}
This doesn't work.
For capturing image you should set orientation too. When you save image to disk you should use
writeImageToSavedPhotosAlbum:orientation:completionBlock:
function and set correct "orientation" parameter there too.
Usage: https://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/index.html#//apple_ref/occ/instm/ALAssetsLibrary/writeImageToSavedPhotosAlbum:orientation:completionBlock:
Example on Objective C:
// Flash set to Auto for Still Capture
[CameraViewController setFlashMode:AVCaptureFlashModeAuto
forDevice:[[self videoDeviceInput] device]];
// Capture a still image.
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo]
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer) {
self.imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
[[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:image.CGImage
orientation:(ALAssetOrientation)[image imageOrientation]
completionBlock:^(NSURL *assetURL, NSError *error) {
if(error == nil) {
NSLog(#"PHOTO SAVED - assetURL: %#", assetURL);
} else {
NSLog(#"ERROR : %#",error);
}
}];
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.
I am currently saving a UIImage to the camera roll this way.
UIImageWriteToSavedPhotosAlbum(finalPicture.image, nil, nil, nil);
But what happens if the user denies us permission to access their photos... how can I tell that this has happened and display an error message?
To save the image to the camera roll I'm Using ALAssetsLibrary so in the method:
//Called after taking the photo with the camera or selected the image from the gallery
- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *referenceURL;
if(Picker.sourceType==UIImagePickerControllerSourceTypeCamera){
UIImage* takenImage=info[UIImagePickerControllerOriginalImage];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[takenImage CGImage] orientation:(ALAssetOrientation)[takenImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
//NOT SAVED
//DISPLAY ERROR THE PICTURE CAN'T BE SAVED
} else {
//SAVED
}
}];
}
}else{
//Selected from gallery
}
Also remember that you have to check first if the camera is available.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//The camera is available
}else{
//No camera available
}