How to load image from UIImagePicker with alpha - ios

I am using UIImagePicker to load an image that I have upload to the photo library (on my iPad), however it loads without the alpha channel.
I have tripple checked to make sure the image has one. Unless it is removed when syncing to itunes?
Here is the code I am using for the image picker when it finishes picking:
-(void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//dissmiss picker
[imagePopOver dismissPopoverAnimated:YES];
//get the picker image
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//create image view for selected image
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
imageView.center = CGPointMake(512, 384);
[imageLayer addSubview:imageView];
//release image and image view
[image release];
[imageView release];
}
What am I doing wrong? I hope this isn't a 'Doh!' moment.

UIImagePicker does support images with an alpha channel, as Kangoo states in their own answer.
However, the conditions for it working correctly are more complicated than just downloading an image through browser to photo lib.
First of all, it makes most sense to be using iOS's image picker to be picking only from the Camera Roll. On an iOS device, it's possible to save images from Mail or Safari to the Camera Roll, and other apps that support images with an alpha channel allow you to save to the Camera Roll.
Images in the rest of the Photos Library, other than the Camera Roll, would have gotten there by iTunes sync. And that's a problem. Although iPhoto on OSX does support PNG images with alpha channel, when iTunes sync pushes those images to an iOS device the transparent areas of the image background become opaque white.
Second, if you configure iOS's image picker to allow editing (square crop, move, scale), then the transparent areas of the image background will become opaque black!
But if you configure iOS's image picker to NOT allow editing, and you pick an image from the Camera Roll, then the picked image (UIImage) can have an alpha channel.
On the picked UIImage, use CGImageGetAlphaInfo(pickedImage.CGImage) to get the CGImageAlphaInfo, and if that info is not (kCGImageAlphaNone or kCGImageAlphaNoneSkipFirst or kCGImageAlphaNoneSkipLast) then the image has an alpha channel.

Never mind. It was as I suspected. The alpha channel is lost somehow when importing the png through itunes (syncing). Must be converting to 24bit? I uploaded my image to the web and then downloaded it through browser to photo lib and it's fine now.

Have you tried calling:
imageView.backgroundColor = [UIColor clear]
Probably you may need to make sure that imageLayer is transparent, too. I am not sure how this has been implemented.

Related

Want fixed orientation, but the UIImage autoratate

I am confused about the UIImage orientation in my iOS design.
I simply load an image taken by my iPhone into UIImageView using storyboard and expect it would be shown exactly the same in the simulator. However, it rotates.
(I choose the content mode to be aspect fit)
I try with other images downloaded from the internet all of them works fine but the one was taken by the camera.
Anyone have any idea why this happens?
edit:
I try to print out imageOrientation property my example. It shows 0 which is the default value of .up.
It seems the picture has not been rotated but it looks different in storyboard and simulator.
The example would be as following:
The UIImage is rotated automatically , the developer should rectify it.
use the below function to get the image in the correct orientation.
UIImage *imageToDisplay = [UIImage imageWithCGImage:[originalImage CGImage] scale:[originalImage scale] orientation: UIImageOrientationUp];
A UIImage has a property imageOrientation, which instructs the UIImageView and other UIImage consumers to rotate the raw image data. There's a good chance that this flag is being saved to the default data in the uploaded jpeg image, but the program you use to view it is not honoring that flag. Try changing the orientation while fetching image from UIImage Picker Controller.
To rotate the UIImage to display properly when uploaded, you can use a category. Check #Anomie's ans in Objective C. If you are using Swift you can create your extension for UIImage using same logic.

iOS: saving UIImageView to Camera Roll

Even though this sounds super simple, I got stuck on this because of how my app works.
I have a UIImageView that loads an online image. On the app, I will have a button that gives the user a choice to export the image to Camera Roll.
Most solutions such as UIImageWriteToSavedPhotosAlbum don't work with UIImageView.
Should I first capture a UIImage from the UIImageView and then save to Camera Roll? How? Or can I save directly?
The UIImage is a representation of an image. UIImageView is a container for displaying that image in your app. What you need to save is the image, not the container.

How to get the size of image & video from UIimagepicker in iOS?

I am picking a image & video using UIImagePickController.I want to ge the size of that image or video so that i can restrict user for uploading large files.I am trying following code for image but it gives me wring data.Size of image is 1.5MB but it always show 13.6 MB.Below is the code for that.
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
imageData = UIImagePNGRepresentation(originalImage);
NSLog(#"%f",(float)[imageData length]/1024.0f/1024.0f);

How to override the automatic re-orientation of images in iOS?

iOS 7, Xcode 5
I'm loading photos into a UIImageView and noticed that they are automatically re-oriented to display "face-up". You can see this same effect by taking 4 pictures with your phone, each time rotating the phone 90 degrees more, then viewing the images on the phone.
The problem is that I'm trying to implement some rotations after loading the image and it has the wrong effect.
For example, I have a photo taken with the iPhone vertical (home button at bottom).
When I copy the image into a UIImageView it is re-oriented face-up (without my code).
I do not want this.
And when I make another copy and apply a rotation, the rotation appears to be against the original tmp image!
What I am trying to get is an image and it's mirror.
Here's my code:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
imageTmp=info[UIImagePickerControllerOriginalImage];
NSLog(#"original orientation:%li",imageTmp.imageOrientation);
self.imageA.image=imageTmp;
self.imageB.image=[UIImage imageWithCGImage:[imageTmp CGImage]
scale:1.0
orientation:UIImageOrientationUpMirrored];
}
In the screenshot above, the image on the left was taken with the phone Vertical.
I then load it into a UIImage.
Next I copy it into a UIImageView "imageA".
Last, I transform and copy the "imageTmp" into another UIImageView "imageB".
Notice that the Left image is auto-re-oriented (I do not want this!), while the right image is transformed based on the actual landscape version.
How can I prevent the auto-re-orientation?
You can get the image without any implied rotation, by creating a new UIImage from the CGImage and not specifying an orientation:
naturalImage = [UIImage imageWithCGImage:[originalImage CGImage]];
Note that, as I mention in the comments above, this is always going to effectively be LandscapeRight (I think, maybe LandscapeLeft, I'm too lazy to remember which is which), as that is how the camera records the images.

Cropping UIImage from photo library vs iPhone camera

I can crop images from the iPhone's photo library no problem like this:
CGRect topRect = CGRectMake(0, 0, image.size.width, image.size.height / 2);
CGImageRef topCroppedCGImageRef = CGImageCreateWithImageInRect(image.CGImage,
topRect);
UIImage *croppedImage = [[UIImage alloc] initWithCGImage:topCroppedCGImageRef];
CGImageRelease(topCroppedCGImageRef);
However this doesn't work when the image comes from the camera. Specifically the cropped image is rotated and the cropped portion isn't as expected. After reading around it sounds like this problem is relatively common. However I've tried the various code fixes and it's not quite working (still have rotation, unexpected cropping and even distortion issues). So I'd like to actually understand why the above cropping doesn't just work for images coming from the camera.
Why doesn't the above cropping method work on images coming from the iPhone's camera?
As pointed out by this famous post - Resize a UIImage the right way, this is because you leave out functionality such as EXIF orientation support, an absolute necessity when dealing with photographs taken by the iPhone’s camera.
By default (picture taken in portrait) the image has an EXIF orientation flag = 6 which means the image is rotated 90 degrees counterclockwise:
$ identify -format "%[EXIF:orientation]" myimage.jpg
6

Resources