In ios, by default while saving the image. It will renders 72 ppi image. But i want 200 or more dpi image.Resolution should not be change.
For example if i have 1200x1200 dimension image, while saving image it is 72 ppi but i wanted it as 200 or more ppi with same 1200x1200 dimensions
Code:
CGSize finalCanvasSize = CGSizeMake(1200*1200);
UIGraphicsBeginImageContextWithOptions(finalCanvasSize, NO, 0.0);
[canvasImage drawInRect:CGRectMake(0,0,finalCanvasSize.width,finalCanvasSize.height)];
resizedCanvasImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Related
I'm trying to use UIGraphicsImageRenderer to fix the orientation of images before uploading to my web server, and the following code works but the images it produces have a higher PPI than I want:
let imageRendererFormat = UIGraphicsImageRendererFormat()
let imageRenderer = UIGraphicsImageRenderer(size: image.size, format: imageRendererFormat)
let pngData = imageRenderer.pngData(actions: { context in
let rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
image.draw(in: rect)
})
The images that are produced have the correct resolution, but the PPI is 216 instead of the expected 72. I observed that the original image's scale is 1.0, but the scale of context.currentImage is 3.0. I'm not sure where this 3.0 number is coming from. This probably explains the 216 PPI, though, since 3 x 72 = 216.
How can I fix the PPI of the images being rendered? Should I apply a scale factor to the context or something?
I figured it out on my own - UIGraphicsImageRendererFormat has a 'scale' property which defaults to the same scale as the main screen.
I had to explicitly set the scale to 1.0 in order to get an image with the correct PPI.
I have two UIImageView with dimension 64 x 16 and two images (#2x):
image1 (128 x 32)
image2 (80 x 32).
I've placed image1 in imageview1 and image2 in imageview2.
When I run the app, imageview1 looks ok, but imageview2 looks distorted. How to deal with different image sizes? I want the UIImageView size to be fixed (64 x 32).
Any help would be really appreciated
image1 with dimension 128 x 32 and image2 with dimension 80 x 32
Your 2x image should have 160 x 64, not 128 x 32.
Edit:
If you have a 64x16 points of UIImageView, that results in 128 x 32 pixels on a 2x scale screen, if you have an image with 80 x 32 pixels, that will get disorted if you try to put it inside a 64x16 point (128x32 pixels) UIImageView
I'm doing some basic line drawing using UIGraphicsBeginImageContextWithOptions:
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let path = UIGraphicsGetCurrentContext()
path!.setLineWidth(1.5)
....
x = (az / 360) * Double(size.width)
y = halfheight - Double(alt / 90 * halfheight)
path?.addLine(to: CGPoint(x:x, y:y))
path?.strokePath()
....
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//DEBUGGING - makes quick look work
let _ = image!.cgImage
return image!
The debugger reports the right size for the image, 1440x960. But when I use QuickLook, the image is 2880 × 1920 pixels. I'm curious if there is an obvious reason for this? Is this something that quicklook is doing, or Preview perhaps?
The debugger is most likely giving you the size in points and Quick Look will be giving you the size in pixels.
The problem is that by passing 0 in the scale parameter of UIGraphicsBeginImageContextWithOptions, you're using the device's screen scale for the scale of the context. Thus if you're on a device with a 2x display, and input a size of 1440 x 960, you'll get a context with 2880 x 1920 pixels.
If you want to work with pixels instead of points, then simply pass 1 into the scale parameter:
UIGraphicsBeginImageContextWithOptions(size, false, 1)
Or use the equivalent UIGraphicsBeginImageContext(_:) call:
UIGraphicsBeginImageContext(size)
I have UIImage with size 7017x4962 and have line points A(532,1118) and B(4114,1118).I have reduced the UIImage size to 2339x1654.How can I transform the line points according to new UIImage size.
Multiply x and y proportionate to the new image width and height for each coordinate pair:
x * (2339.0f / 7017.0f)
y * (1654.0f / 4962.0f)
This will give you A(177, 372) and B(1371, 372).
Using the imagemagick convert command, how can I resize an image to a fixed width and proportional height e.g. using the -resize or the -thumbnail option?
Use -resize 100x to resize images to 100 pixels in width while maintaining the height's aspect ratio.
Read the fine ImageMagick manual for details.
Imagemagick geometry: 'width'x'height'
If you leave one part empty, this means resize proportional.
Examples:
100x200 # width = 100, height = 200
300x # width = 300, height = proportional
x300 # width = proportional, height = 300