How to add an Image to plot symbol in core plot - core-plot

I am using the scatter plot and I need to have images as plot symbol. I am using CPTFILL to fill the image for plotsymbol . However I get a crash in "-(nonnull instancetype)initWithCGImage:(nullable CGImageRef)anImage scale:(CGFloat)newScale" that says "Assertion failure in -[CPTImage initWithCGImage:scale:]".
Here is the code :
public func symbol(for plot: CPTScatterPlot, record idx: UInt) -> CPTPlotSymbol? {
let fillImage = CPTImage(named: "heart")
plotSymbol.fill = CPTFill(image: fillImage)
plot.plotSymbol = plotSymbol
return plotSymbol
}

You'll see this assertion failure if the app couldn't find the image file. CPTImage uses the -[UIImage imageNamed:] method internally.
Make sure that the image is properly compiled into the app bundle. Add the file extension (e.g., .jpg) to the image name if it is not .png.

Related

UIImage.pngData(_:) losing Camera photo orientation [duplicate]

This question already has answers here:
Swift PNG Image being saved with incorrect orientation
(3 answers)
Closed 2 years ago.
I'm learning how to use UIImagePickerController and got stuck with a problem using UIImagePickerController.sourceType = .camera.
What my app is supposed to do is:
to allow a user to take a photo using the system view controller mentioned above
then convert this image using UIImage.pngData(_:)
save this data to an appropriate struct field (doesn't matter which one, in that case)
use data saved to the structure to make up an image and set this image as a UIButton foreground image
When i'm doing so (following App Development with Swift book's example project) image appears to be rotated 90 degrees for some reason, which I'd like to know.
I've tried to create additional UIImageView and set its image property before converting UIImage to pngData, and it appeared normally (see 2nd screenshot).
When choosing a photo from the photo library problem does not occur in any of the cases (before-after converting).
So I suppose, that pngData is somehow losing photo-orientation information? Or I've messed somewhere else, perhaps.
Here are screenshots from my app, so you can see how original taken photo looks, and how it looks in-app (above labels - UIButton, below - test UIImageView). Nevermind text :)
If you save UIImage as a JPEG, this will set the rotation flag.
PNGs do not support a rotation flag, so if you save a UIImage as a PNG, it will be rotated incorrectly and not have a flag set to fix it. So if you want PNGs you must rotate them yourself.
let jpgData = downloadedImage.jpegData(compressionQuality: 1)
To get rotated image you need to draw that.. you can use following extension
extension UIImage {
func rotateImage()-> UIImage? {
if (self.imageOrientation == UIImage.Orientation.up ) {
return self
}
UIGraphicsBeginImageContext(self.size)
self.draw(in: CGRect(origin: CGPoint.zero, size: self.size))
let copy = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return copy
}
}
How to use
let rotatedImage = downloadedImage.rotateImage()

How to convert a PNG to a WebP in Swift?

I'm trying to convert a PNG image in Webp in Swift, the only framework I got working is OpenCV through Objective-c++. The problem is that if I resize the image by 512x512 (that's the resolution I need) it crashes:
If I resize the image (either with OpenCV either with Swift) to another resolution (ex 510x510) it doesn't crash.
The strange thing is that on the simulator it never crashes while on the iPhone XS it crashes 90% of the times. How can I convert a PNG to a Webp in Swift? Why is OpenCV crashing on the imwrite instruction if the Mat is 512x512?
UPDATE:
OpenCV version: 3.4.2
I found out that this problem happens when the PNG image get processed before from the Core Graphics framework. I need to use it since I save a UIVIew as UIImage this way:
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
Swift 4.0
pod both 'SDWebImage' ( https://github.com/SDWebImage/SDWebImage ) AND 'SDWebImage/WebP' ( https://github.com/SDWebImage/SDWebImageWebPCoder )
import SDWebImage
if let image_download = UIImage(data: data) {
let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
}
I ended up using another framework to convert PNG to WebP: https://github.com/seanooi/iOS-WebP, had to create the wrapper to use it on swift but it works very good 😊
My wrapper is very simple but does what I needed:
#import <Foundation/Foundation.h>
#import "WebPWrapper.h"
#import "UIImage+WebP.h"
#implementation WebPWrapper
-(NSData *)convertUIImageToWebp:(UIImage *)image :(int)quality {
NSData *webPData = [UIImage imageToWebP:image quality:quality];
return webPData;
}
#end
In swift I use it this way:
let webPData = WebPWrapper().convertUIImage(toWebp: image, 90)

iOS Share Extension Changing Size of Preview Image with Swift

In an iOS Share Extension it is possible to grab the preview image with loadPreviewImageWithOptions. Is it also possible to change the size of the preview image?
The preview image seems to be a screenshot of the website. I am wondering if it is possible to get the screenshot with the original size.
As documented a dictionary can be created for the options.
https://developer.apple.com/library/prerelease/ios/documentation/Foundation/Reference/NSItemProvider_Class/index.html#//apple_ref/doc/constant_group/Options_Dictionary_Key
For this reason I am using following code (trying with different values).
let options: [String: NSValue] = [NSItemProviderPreferredImageSizeKey: NSValue(CGSize: CGSizeMake(375.0, 600.0))]
itemProvider.loadPreviewImageWithOptions(options, completionHandler: { [ weak self ] image, error in
if let image = image as? UIImage {
}
})
But the result is always an image with the size of 84 x 79
Thanks!

Write UIImage to PNG; read PNG into UIImage; resulting image has twice the resolution

Following these steps I'm getting an unexpected result:
1) Write UIImage to PNG
2) Read PNG into UIImage
Result: the sizes of the UIImages (before and after PNG) are different. The UIImage created by reading from the PNG has twice the resolution as the original UIImage used to create the PNG.
Swift pseudocode follows:
var initialImage : UIImage;
// Obtain UIImage from UIImagePickerController, Images.xcassets, ...
// As an example, consider a UIImage with size = (420.0, 280.0).
println("Image size = \(initialImage.size)") // Indicates "Image size = (420.0, 280.0)"
// Write image to file
UIImagePNGRepresentation(myImage).writeToFile(imagePath, atomically: true)
// Retrieve image from file
let newImage = UIImage(contentsOfFile: imagePath)!
println("Image size = \(initialImage.size)") // Indicates "Image size = (840.0, 560.0)"!
All is well otherwise. Displaying the newly read image works great.
I'm running this connected to an iPhone 6. Any thoughts would be greatly appreciated.
gary
It's converting it for the retina display. Each point is represented by 2 pixels on the screen so the saved image is double the resolution. This is a good article to reference: http://www.paintcodeapp.com/news/iphone-6-screens-demystified

trying to load a bitmap, but xna want´s a Texture2D

I'm currently trying to load a simple bitmap using XNA but I get the following error:
Error loading "Maps\standard". File contains Microsoft.Xna.Framework.Graphics.Texture2D but trying to load as System.Drawing.Bitmap.
code:
public Bitmap map;
public void load(Game game, String image) {
path = image; //path to image
map = game.Content.Load<Bitmap>("Maps/"+path);
sizeX = map.Width;
sizeY = map.Height;
}
You want the below:
map = game.Content.Load<Texture2D>("Maps/"+path);
The way XNA works is that there is a content pipeline, which takes inputs (like your bitmap image) and produces outputs (the Texture2D), which is in a different format to the input.
XNA works with a Texture2D object when displaying images.
Now, I just use the C# standard
Bitmap bmp = (Bitmap)Bitmap.FromFile(path);

Resources