GLKView snapshot returns distorted image - ios

I am using CIFunHouse apple demo for my project to applying filter effects, When I try to take a snapshot of GLKView on iPad Air by making
UIImage* imageCaptured = [(GLKView*)_videoPreviewView snapshot];
the UIImage generated have distortion. see image
Any ideas? How to fix this.
Thanks in advance.

Related

Changes in imageWithCGImage:scale:orientation api ios13 with respect to gif images

I am using some custom code to render gif in UIImageView. Below is the link for the code used
https://github.com/swiftgif/SwiftGif/blob/master/SwiftGifCommon/UIImage%2BGif.swift
I have changed the code a bit to respect scale for different resolution devices. Below is the code.
frame = UIImage(cgImage: images[Int(index)], scale: UIScreen.main.scale, orientation: .up)
Till iOS12 this was working fine and the image got scaled and down scaled based on device. From iOS13, if #2x, #3x images are used, then they are getting scaled and also scale is not being respected.
Tried few other gif rendering techniques on iOS, but no use. Can anyone suggest what's happening with the api, if they have encountered the same problem.
This issue got resolved when I set array of images directly to "animatedImages" property rather than creating UIImage using
"[UIImage animatedImageWithImages:frames duration:(NSTimeInterval)]" api.

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.

What size has a background image to have?

I'm developing a game in Swift (but not sprite kit), and i'm having a lot of trouble to make a background image that look great in every device. I recently understand the concept of the #1x, #2x and #3x images and it's pixels, but i can't find any place that say what size (width and height) an background image must have to look good both in canvas mode, preview mode and in action.
Anyone can help me?
For help, i'm attaching two print screens of a test that i made.
Thanks for the help :)
Image on Xcode 7 canvas
image in preview mode. Notice how he cut the edges
Size Icon For Devices:
UIImageView *imageView = [UIImageView alloc]init];
imageView.frame = CGRectMake(0,0,widthInt,heightInt);
imageView.image = [UIImage ImageWithName:#"nameImage.png"];

Force NSView drawing to behave as if display is retina

I've got a custom NSView which draws a chart in my app. I am generating a PDF which includes the image. In iOS I do this using code like this:
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
[self drawRect:self.frame];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
In iOS, the displays are retina which means the image is very high resolution, however, I'm trying to do this in my Mac app now and the quality of the image is poor because non-retina Macs will generate a non-high res version of the image.
I would like to force my NSView to behave as if it was retina when I'm using it to generate an image. That way, when I put the image into my PDF, it'll be much higher resolution. Right now, it's very blurry and not attractive.
Even a Retina bitmap will still be blurry and unattractive when scaled up enough. Assuming the view draws its contents in drawRect:, rather than trying to render the view into a PDF at a fixed resolution, a better approach is to draw directly into a PDF graphics context. This will produce a nice scalable PDF. The drawing code will need to be factored so it can be used for both the view’s drawRect: and the PDF.
Also, the iOS documentation states you should never call drawRect: yourself. Call renderInContext: on the view‘s layer, or use the newer drawViewHierarchyInRect:afterScreenUpdates:.
You can call -[NSView dataWithPDFInsideRect:] to get PDF data from the drawing in a view. For example:
NSData* data = [someView dataWithPDFInsideRect:someView.bounds];
[data writeToFile:#"/tmp/foo.pdf" atomically:YES];
Any vector drawing (e.g. text, Bezier paths, etc.) that your view and its subviews do will end up as scalable vector graphics in the PDF.

How to force -drawViewHierarchyInRect:afterScreenUpdates: to take snapshots at #2x resolution?

-drawViewHierarchyInRect:afterScreenUpdates: is a fast way in iOS 7 to take a snapshot of a view hierarchy.
It takes snapshots but with #1x resolution. The snapshots look pixellated and blurry on an iPhone 5S. The view from which I create a snapshot is not transformed.
I don't want to blur it and want good quality as seen on screen.
Here is how I capture it:
UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I also tried:
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, self.contentScaleFactor);
which still renders with low #1x quality.
Is there another way to configure the image context so it's #2x resolution?
The correct one would be:
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
OK, so the problem was that self.contentScaleFactor returns a wrong value on a retina display device. It is 1 where it should be 2.
This works, but of course it's less than ideal because it has no fallback for non-retina devices.
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 2);

Resources