Flipping image not working on iOS 10 - objective-c

I am facing weird issue while flipping Image in iOS 10
Here is my code.
uiImage = [[UIImage alloc] initWithCIImage:ciImage];
flippedImg = [UIImage imageWithCIImage:ciImage scale:uiImage.scale orientation:UIImageOrientationDownMirrored];
captureImgView.image = flippedImg;
I am taking one ciImage and getting it into uiImage.
then flipping that UIImage.
this code works fine on all iOS version, lower than 10.
but not working on 10 and above.
I tried all Orientations,
UIImageOrientationUpMirrored
UIImageOrientationLeftMirrored
UIImageOrientationUp
UIImageOrientationRight
UIImageOrientationDown
UIImageOrientationLeft
UIImageOrientationDownMirrored
UIImageOrientationRightMirrored
But nothing work.
I am taking selfie from front camera and attaching that selfie to UIImageView.
I don't want to attach flipped image taken from front camera.
I want orignal image here.
Please guide me for same.
Thanks in advance.

I got the solution by my own.
What I did,
I checked for iOS10 version of system,
captured the image from front camera, converted from CIImage to UIImage and placed it on the UIImageView.
Once I placed it on UIImageView,
I rotate the UIImageView using CGAffineTransformMakeRotation to fit in appropriate position and place.
here is the sample code.
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
captureImgView = [[UIImageView alloc]init];
if([systemVersion floatValue] < 10.0)
{
uiImage = [[UIImage alloc] initWithCIImage:ciImage];
flippedImg = [UIImage imageWithCIImage:ciImage scale:uiImage.scale orientation:UIImageOrientationDownMirrored];
captureImgView.image = flippedImg;
captureImgView.transform = CGAffineTransformMakeRotation(M_PI_2);
}
else
{
uiImage = [[UIImage alloc] initWithCIImage:ciImage];
flippedImg = [UIImage imageWithCIImage:ciImage scale:uiImage.scale orientation:UIImageOrientationUpMirrored];
captureImgView.image = uiImage;
captureImgView.transform = CGAffineTransformMakeRotation ((M_PI * 270 / 180.0));
}

I think you missed something. In my code it's working fine...
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *imgOriginal;
#property (weak, nonatomic) IBOutlet UIImageView *imgFlipped;
#end
#pragma mark - UIImagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *uiImage = info[UIImagePickerControllerOriginalImage];
CGImageRef cgImage = uiImage.CGImage;
CIImage *ciImage = [CIImage imageWithCGImage:cgImage];
UIImage *flippedImg = [UIImage imageWithCIImage:ciImage scale:uiImage.scale orientation:UIImageOrientationRight];
_imgOriginal.image = uiImage;
_imgFlipped.image = flippedImg;
[self dismissViewControllerAnimated:YES completion:nil];
}
Here output :
In iOS 10, it will be like :
#pragma mark - UIImagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *uiImage = info[UIImagePickerControllerOriginalImage];
CGImageRef cgImage = uiImage.CGImage;
//CIImage *ciImage = [CIImage imageWithCGImage:cgImage];
//UIImage *flippedImg = [UIImage imageWithCIImage:ciImage scale:uiImage.scale orientation:UIImageOrientationRight];
UIImage *flippedImg = [UIImage imageWithCGImage:cgImage scale:uiImage.scale orientation:UIImageOrientationLeftMirrored];
_imgOriginal.image = uiImage;
_imgFlipped.image = flippedImg;
[self dismissViewControllerAnimated:YES completion:nil];
}

Related

How To Speed Up Image Blur?

I have the following code in my viewWillAppear in a modal uiviewcontroller.
I am including UIImage+ImageEffects.h to do the blurring of the background image in this example.
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:NO];
// grab an image of our parent view
UIView *parentView = self.presentingViewController.view;
UIImage *parentViewImage = [self takeSnapshotOfView:parentView];
UIImage *blurredImage = nil;
//BLUR THE IMAGE
blurredImage = [self blurWithImageEffects:parentViewImage];
// insert an image view with a picture of the parent view at the back of our view's subview stack...
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = blurredImage;
[self.view insertSubview:imageView atIndex:0];
}
[EDIT] Adding Blur Methods
- (UIImage *)takeSnapshotOfView:(UIView *)view
{
CGFloat reductionFactor = 1.5;
UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor));
[view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor) afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)blurWithImageEffects:(UIImage *)image
{
return [image applyBlurWithRadius:10 tintColor:[UIColor colorWithWhite:1 alpha:0.2] saturationDeltaFactor:1.5 maskImage:nil];
}
The code works fine and the background shows up blurry but it is really noticeably slow on iPad 3 with iOS 8. When the button to display this viewcontroller is tapped there is a pause before the viewcontroller slides up from the bottom. If I remove the blur the view controller slides up faster.
I tried putting the code in the viewDidAppear but then there is a noticeable white background displayed for a few seconds before the blur appears. But when in viewDidAppear the viewcontroller slides up immediately.
If I reduce the applyWithBlurRadius value this does not seem to reduce the time it takes to apply the blur.
Is there any way I can make it run faster?
GPUImage might solve your issue, Its fast and it doesn't put too much load on processing.
Link To Framework : https://github.com/BradLarson/GPUImage
Link to use GPUImage Blur
http://blog.bubbly.net/tag/gpu-image/
http://www.raywenderlich.com/60968/ios-7-blur-effects-gpuimage
However a reference is shown below which will give you the basic idea:
First prepare your view which is to blurred:
- (NSData *)PhotoForBlurring:(UIView *)view{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:#"aName.png" atomically:YES];
return data; }
-(void)sharingScrapPage{
GPUImageiOSBlurFilter *blur = [[GPUImageiOSBlurFilter alloc]init];
blur.blurRadiusInPixels=2.0;
blur.downsampling=6.0;
[blurview removeFromSuperview];
blurview = [[UIImageView alloc]initWithFrame:getRectDisplay(0, 0, 480, 320)]; // Landscape Iphone 4S
UIImage *imageForBlurring=[UIImage imageWithData:[self PhotoForBlurring:self.view]];
NSData* pngdataForBlurring = UIImagePNGRepresentation (imageForBlurring);
UIImage* blurImage = [UIImage imageWithData:pngdataForBlurring];
blurImage=[blur imageByFilteringImage:blurImage];
blurview.image=blurImage;
[self.view addSubview:blurview];
[progressHUD removeFromSuperview];
progressHUD = [[ProgressHUD alloc]initWithFrame:getRectDisplay(190, 110, 100, 90)];
[blurview addSubview:progressHUD]; }

Crop UIImagePicker and output to CollectionView cell

I am trying to create a camera button that takes a square picture that then fills a CollectionViewController cell. Right now, I'm having a little trouble cropping the picture and outputting it to the cell. In the lines of code commented "Crop the image to a square: I'm getting an error saying that "image" is an undeclared identifier. How do I crop the image and set it to a cell?
- (IBAction)cameraButtonClicked:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
UIAlertView *cameraAlertView = [[UIAlertView alloc] initWithTitle:#"Camera Not Available" message:#"There is no camera on this device which really defeats the purpose of this game. We suggest you get an iDevice with a camera." delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[cameraAlertView show];
}else{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
if (ipc.sourceType == UIImagePickerControllerSourceTypeCamera) {
//Create camera overlay
CGRect f = ipc.view.bounds;
f.size.height -= ipc.navigationBar.bounds.size.height;
CGFloat barHeight = (f.size.height - f.size.width) / 2;
UIGraphicsBeginImageContext(f.size);
[[UIColor colorWithWhite:0 alpha:.5] set];
UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, barHeight), kCGBlendModeNormal);
UIRectFillUsingBlendMode(CGRectMake(0, f.size.height - barHeight, f.size.width, barHeight), kCGBlendModeNormal);
UIImage *overlayImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *overlayIV = [[UIImageView alloc] initWithFrame:f];
overlayIV.image = overlayImage;
[ipc.cameraOverlayView addSubview:overlayIV];
}
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
//Crop the image to a square
CGSize imageSize = image.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
if (width != height) {
CGFloat newDimension = MIN(width, height);
CGFloat widthOffset = (width - newDimension) / 2;
CGFloat heightOffset = (height - newDimension) / 2;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), NO, 0.);
[image drawAtPoint:CGPointMake(-widthOffset, -heightOffset)
blendMode:kCGBlendModeCopy
alpha:1.];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
}
#pragma mark ImagePicker Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Move your "Crop" coded below this line:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; in
didFinishPickingMediaWithInfo
You will need to update your dataSource with the newly cropped image. You can then either call reloadData or insertItemsAtIndexPaths: on your UICollectionView to have it reflect the newly added image. I would side with calling the latter method as it will be more efficient than reloading the entire collection with reloadData (when there are lots of images in the collection).
UICollectionView reference is here.

XCode Custom Activity Indicator - Hide after UIWebView Loads (webViewDidFinishLoad)

I'm using the code found here http://blog.blackwhale.at/?p=336 to create a custom activityIndicator in the current version of xCode. This code uses a series of .png images in an array and then displays them at the set interval to create an animated loading image that you can then place on the screen (and assuming you can somehow remove it) whenever you want.
In my main ViewController.m file I have the following code in my viewDidLoad section :
/* --- START CUSTOM ACTIVITY INDICATOR */
//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:#"activity1.png"];
UIImageView *activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"activity1.png"],
[UIImage imageNamed:#"activity2.png"],
[UIImage imageNamed:#"activity3.png"],
[UIImage imageNamed:#"activity4.png"],
[UIImage imageNamed:#"activity5.png"],
[UIImage imageNamed:#"activity6.png"],
[UIImage imageNamed:#"activity7.png"],
[UIImage imageNamed:#"activity8.png"],
[UIImage imageNamed:#"activity9.png"],
[UIImage imageNamed:#"activity10.png"],
[UIImage imageNamed:#"activity11.png"],
[UIImage imageNamed:#"activity12.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 0.6;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectMake(
self.view.frame.size.width/2
-statusImage.size.width/2,
self.view.frame.size.height/1.2
-statusImage.size.height/1.2,
statusImage.size.width,
statusImage.size.height);
//Start the animation
[activityImageView startAnimating];
//Add your custom activity indicator to your current view
[self.view addSubview:activityImageView];
/* --- END CUSTOM ACTIVITY INDICATOR */
I would like to CLEAR/DELETE/HIDE the activityImageView element, as I only want it to show when the app first launches until the UIWebView finishes loading.
I would like to call this and have the gif appear while webViewDidStartLoad and then clear when webViewDidFinishLoad. Someone help??
You should go like this:
- (void)viewDidLoad {
[super viewDidLoad];
//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:#"activity1.png"];
UIImageView *activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"activity1.png"],
[UIImage imageNamed:#"activity2.png"],
[UIImage imageNamed:#"activity3.png"],
[UIImage imageNamed:#"activity4.png"],
[UIImage imageNamed:#"activity5.png"],
[UIImage imageNamed:#"activity6.png"],
[UIImage imageNamed:#"activity7.png"],
[UIImage imageNamed:#"activity8.png"],
[UIImage imageNamed:#"activity9.png"],
[UIImage imageNamed:#"activity10.png"],
[UIImage imageNamed:#"activity11.png"],
[UIImage imageNamed:#"activity12.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 0.6;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectMake(
self.view.frame.size.width/2
-statusImage.size.width/2,
self.view.frame.size.height/1.2
-statusImage.size.height/1.2,
statusImage.size.width,
statusImage.size.height);
//Start the animation
[activityImageView startAnimating];
//Add your custom activity indicator to your current view
[self.view addSubview:activityImageView];
self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
target:self
selector:#selector(loading)
userInfo:nil
repeats:YES];
}
- (void)loading {
if (!self.webView.loading) {
[activityImageView stopAnimating];
activityImageView.hidden = 1;
} else {
[activityImageView startAnimating];
}
}
Okay, so the reason that you cannot access activityImageView is because its declared in your viewDidLoad function. Add it as a property to your ViewController class and you should be able to do what you want. Make sure that you remove the declaration of activityImageView from viewDidLoad, and just initialize it there.
Also, where is your webview that you want to show the activity indicator? Does it have a separate view controller or is it included in your main view controller? It sounds like your scope for activityImageView is incorrect, though without knowing how you have everything set up its a little hard for me to tell you where to put it.
After you use your activity indicator in these two places, do you plan on using it again during the applications run time, or will it only ever be visible if you reload the application? Depending on the answer to that you will either want to just hide it, or remove it from you main view.
UIImageView.hidden is what controls visibility.
EDIT, Code as requested:
ViewController.h
#interface ViewController : UIViewController
{
UIWebView *_webView;
}
#property(nonatomic, strong) UIWebView *webView;
#property(nonatomic, strong) UIImageView *activityImageView;
ViewController.m
#implementation ViewController
#synthesize activityImageView;
#synthesize webView = _webView;
-(void)viewDidLoad {
//all your previous stuff with the change that you just alloc activityImageView instead of declare it
activityImageView = [[UIImageView alloc] initWithImage:statusImage];
//The above is the initialization, below is where your old code should go
}
Declare your activityImageView as a property in your header file and configure it in your viewDidLoad: method
You should add your activityImageView in UIWebViewDelegate method
- (void)webViewDidStartLoad:(UIWebView *)webView
And then stopAnimating and removeFromSuperview in the UIWebViewDelegate methods
- (void)webViewDidFinishLoad:(UIWebView *)webView
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
You should read UIImageView documentation and the UIWebViewDelegate protocol reference
UPDATE
In your header:
#property(nonatomic, retain) UIImageView *activityImageView;
In your implementation
#synthesize activityImageView;
-(void)viewDidLoad{
activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"activity1.png"],
[UIImage imageNamed:#"activity2.png"],
[UIImage imageNamed:#"activity3.png"],
[UIImage imageNamed:#"activity4.png"],
[UIImage imageNamed:#"activity5.png"],
[UIImage imageNamed:#"activity6.png"],
[UIImage imageNamed:#"activity7.png"],
[UIImage imageNamed:#"activity8.png"],
[UIImage imageNamed:#"activity9.png"],
[UIImage imageNamed:#"activity10.png"],
[UIImage imageNamed:#"activity11.png"],
[UIImage imageNamed:#"activity12.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 0.6;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectMake(
self.view.frame.size.width/2
-statusImage.size.width/2,
self.view.frame.size.height/1.2
-statusImage.size.height/1.2,
statusImage.size.width,
statusImage.size.height);
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
[self.activityImageView startAnimating];
[self.view addSubview:activityImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[activityImageView stopAnimating];
[activityImageView removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[activityImageView stopAnimating];
[activityImageView removeFromSuperview];
}

UIImageView setImage doesn't work

I'm making an animation calling repeatedly this function so I can make an animation:
The method where I have the issue, is a method of a UIImageView's subclass Class
#interface MyCustomImageView : UIImageView {
& it is the following:
- (void) animationShowFrame: (NSInteger) frame {
NSLog(#"frame: %d", frame);
if ((frame >= animationNumFrames) || (frame < 0))
return;
NSData *data = [animationData objectAtIndex:frame];
UIImage *img = [UIImage imageWithData:data];
NSLog(#"%#",img);
NSLog(#"data: %d", [data length]);
[self setImage:img];
}
The problem is that, it seems like the first image is perfectly set, but the rest of them are ignored...
NSLogging them I can see that the images are there & the data is different every time, so, they are loaded ok, even I tried to addSubview to self (the class is UIImageView's child), through a UIImageView & they are showing, but this is not the way cause in that case I'm using too much memory...
Asking for the description in the console, I can see how self.image = nil after setting it...
I don't know what else I can do with this, it's driving me crazy.
Try to subclass to a UIView instead to a UIImageView, and create the UIImageView as a property inside.
#interface CustomView : UIView {
UIImageView *imageView;
Then, in animationShowFrame use that imageView:
- (void) animationShowFrame: (NSInteger) frame {
NSData *data = [animationData objectAtIndex:frame];
UIImage *img = [UIImage imageWithData:data];
[self.imageView setImage:img];
}
Hope it helps!

Capture UIView as UIImage

I have been using this method to convert a UIView into UIImage. i.e. screen snapshot of a view -
#interface UIView(Extended)
- (UIImage *) imageByRenderingView;
#end
#implementation UIView(Extended)
- (UIImage *)imageByRenderingView
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
#end
To use it, I do this -
UIImage *currImage = [self.view imageByRenderingView];
This gives the image representation of the entire UIView. Now I want 2 images, one is of the top half of the UIView and the other is the bottom half. How do I do that?
You can split your UIImage in two by using this code:
CGImageRef topOfImageCG =
CGImageCreateWithImageInRect(currImage.CGImage,
CGRectMake(0,
0,
currImage.size.width,
currImage.size.height / 2.0));
UIImage *topOfImage = [UIImage imageWithCGImage:topOfImageCG];
CGImageRelease(topOfImageCG);