I'm building a swift game and a need to set up some images.
My code works with string or integer:
for var i = 0; i < globalCurrentMembers.count; ++i {
var MembersDefaultName = NSUserDefaults.standardUserDefaults()
MembersDefaultName.setValue(globalCurrentMembers[i].name, forKey: "globalCurrentMembersName\(i)")
MembersDefaultName.synchronize()
}
But I have an error for an image
for var i = 0; i < globalCurrentMembers.count; ++i {
var MembersDefaultImage = NSUserDefaults.standardUserDefaults()
MembersDefaultImage.setValue(globalCurrentMembers[i].image,
forKey: "globalCurrentMembersImage\(i)")
MembersDefaultImage.synchronize()
}
globalCurrentMembers is an array of Member which looks like that:
class Member {
var image = UIImage ()
var name = String ()
var progression = Int()
var round = Int()
var level = Int()
var imageProgression = [UIButton]()
func Init(){
image = UIImage(named: "default.png")!
name = "default"
progression = 0
round = 0
level = 0
}
}
So please can you tell me how doing that.
UIImage does not implement NSCoding protocol, you need to convert UIImage to NSData first.
Change your code to the following:
var imageData = UIImagePNGRepresentation(globalCurrentMembers[i].image)
var myEncodedImageData = NSKeyedArchiver.archivedDataWithRootObject(imageData)
MembersDefaultImage.setObject(myEncodedImageData, forKey: "globalCurrentMembersImage\(i)")
Of course storing image to NSUserDefaults is not the best practice, you should store your image in some file directory.
You can't store images in NSUserDefaults. Have you considered storing just the path to the image, or the name of the image, instead? That would be enormously more efficient, and also work.
Related
iam very new to swiftUI and dont found a way.
I want to create one image (300x100) dyncmicaly by merging three single 100x100 images horizontaly
ImageA(100x100) + ImageB(100x100) + ImageC(100x100) = ImageD(300x100)
Found a way to show them in a HStack but how can is get one new image to send the Date to new function
Regards
Alex
thanks a lot, I tried to use your function but got an error here "Cannot convert value of type 'UIImage?' to expected element type 'UIImage'!
Code should drat 3x zero. in the number-0.png ist just one lagre zero
//
// ContentView.swift
// imagecombine
//
// Created by Alex on 02.08.20.
// Copyright © 2020 Alex. All rights reserved.
//
import SwiftUI
func combineHorizontally(_ images: [UIImage]) -> UIImage? {
guard !images.isEmpty else { return nil }
var size = CGSize.zero
var scale = CGFloat.zero
for image in images {
scale = max(scale, image.scale)
size = CGSize(width: size.width + image.size.width, height: max(size.height, image.size.height))
}
var position = CGPoint.zero
let format = UIGraphicsImageRendererFormat()
format.scale = scale
return UIGraphicsImageRenderer(size: size, format: format).image { context in
for image in images {
image.draw(at: position)
position.x += image.size.width
}
}
}
let redImage = UIImage(named: "number-0.png")
let greenImage = UIImage(named: "number-0.png")
let blueImage = UIImage(named: "number-0.png")
let image = combineHorizontally([redImage, greenImage, blueImage])
struct ContentView: View {
var body: some View {
Image(image)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You can use UIImageGraphicsRenderer and draw the images one after another:
func combineHorizontally(_ images: [UIImage]) -> UIImage? {
guard !images.isEmpty else { return nil }
var size = CGSize.zero
var scale = CGFloat.zero
for image in images {
scale = max(scale, image.scale)
size = CGSize(width: size.width + image.size.width, height: max(size.height, image.size.height))
}
var position = CGPoint.zero
let format = UIGraphicsImageRendererFormat()
format.scale = scale
return UIGraphicsImageRenderer(size: size, format: format).image { _ in
for image in images {
image.draw(at: position)
position.x += image.size.width
}
}
}
You said that there were only images and they were 100 × 100, but the above should work regardless of the number and size (memory permitting, of course).
Anyway, this
let image = combineHorizontally([redImage, greenImage, blueImage])
Results in:
To use that in a context where you don’t want an optional, you can use ! forced unwrapping operator, ?? nil coalescing operator, or some other unwrapping pattern, e.g. guard let, if let, etc.
Alternatively, if you don’t want to deal with optionals at all, you can write a rendition that doesn’t return an optional at all (but also doesn’t detect the error scenario where an empty array was provided):
func combineHorizontally(_ images: [UIImage]) -> UIImage {
var size = CGSize.zero
var scale: CGFloat = 1
for image in images {
scale = max(scale, image.scale)
size = CGSize(width: size.width + image.size.width, height: max(size.height, image.size.height))
}
var position = CGPoint.zero
let format = UIGraphicsImageRendererFormat()
format.scale = scale
return UIGraphicsImageRenderer(size: size, format: format).image { _ in
for image in images {
image.draw(at: position)
position.x += image.size.width
}
}
}
According to the Cocoa Drawing Guide documentation for Images, NSImage can load a Windows cursor .cur file.
But how do I obtain the hotspot needed for NSCursor - initWithImage:(NSImage *)newImage hotSpot:(NSPoint)point; ?
As the documentation also says,
In OS X v10.4 and later, NSImage supports many additional file formats using the Image I/O framework.
So let's grab a sample cursor file and experiment in a Swift playground:
import Foundation
import ImageIO
let url = Bundle.main.url(forResource: "BUSY_L", withExtension: "CUR")! as CFURL
let source = CGImageSourceCreateWithURL(url, nil)!
print(CGImageSourceCopyPropertiesAtIndex(source, 0, nil)!)
Output:
{
ColorModel = RGB;
Depth = 8;
HasAlpha = 1;
IsIndexed = 1;
PixelHeight = 32;
PixelWidth = 32;
ProfileName = "sRGB IEC61966-2.1";
hotspotX = 16;
hotspotY = 16;
}
So, to get the hotspot safely:
import Foundation
import ImageIO
if let url = Bundle.main.url(forResource: "BUSY_L", withExtension: "CUR") as CFURL?,
let source = CGImageSourceCreateWithURL(url, nil),
let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any],
let x = properties["hotspotX"] as? CGFloat,
let y = properties["hotspotY"] as? CGFloat
{
let hotspot = CGPoint(x: x, y: y)
print(hotspot)
}
Output:
(16.0, 16.0)
Is there any way to convert a UIColor to a string?
For example,
var color = UIColor.purpleColor()
var colorString : String = //Purple
If you can help me it would be greatly appreciated.
Thanks
You can get color description
var color = UIColor.purpleColor()
var colorString : String = color.description
println(colorString)
var cgColorRef = UIColor.purpleColor().CGColor
var strColor = CIColor(CGColor: cgColorRef).stringRepresentation()
println(strColor)
Reference from : How to convert UIColor value to a named color string?
I have a NSViewController and a variable num. I want to change the size of the window dynamically according to that variable. Is there any way to do that in swift?
Let's say your window has an IBOutlet named "window", and your dynamic number is named "myDynamicNumber":
func resize() {
var windowFrame = window.frame
let oldWidth = windowFrame.size.width
let oldHeight = windowFrame.size.height
let toAdd = CGFloat(myDynamicNumber)
let newWidth = oldWidth + toAdd
let newHeight = oldHeight + toAdd
windowFrame.size = NSMakeSize(newWidth, newHeight)
window.setFrame(windowFrame, display: true)
}
In Swift 3 to resize the window you use setFrame.
An example from the ViewController:
func resizeWin(size:(CGFloat,CGFloat)){
self.view.window?.setFrame(NSRect(x:0,y:0,width:size.0,height:size.1), display: true)
}
I needed to toggle viewing a text view so I overlaid the window an invisible view - hideRect just short of the text view; in this way I can resize to the smaller (hideRect) and restore later to the original size - origRect. Hide and original rect captured at viewDidLoad(). Swift 3/Xcode 8.3.3
// class global contants
let kTitleUtility = 16
let kTitleNormal = 22
#IBOutlet var hideView: NSView!
var hideRect: NSRect?
var origRect: NSRect?
#IBAction func toggleContent(_ sender: Any) {
// Toggle content visibility
if let window = self.view.window {
let oldSize = window.contentView?.bounds.size
var frame = window.frame
if toggleButton.state == NSOffState {
frame.origin.y += ((oldSize?.height)! - (hideRect?.size.height)!)
window.setFrameOrigin(frame.origin)
window.setContentSize((hideRect?.size)!)
window.showsResizeIndicator = false
window.minSize = NSMakeSize((hideRect?.size.width)!,(hideRect?.size.height)!+CGFloat(kTitleNormal))
creditScroll.isHidden = true
}
else
{
let hugeSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))
frame.origin.y += ((oldSize?.height)! - (origRect?.size.height)!)
window.setFrameOrigin(frame.origin)
window.setContentSize((origRect?.size)!)
window.showsResizeIndicator = true
window.minSize = NSMakeSize((origRect?.size.width)!,(origRect?.size.height)!+CGFloat(kTitleNormal))
window.maxSize = hugeSize
creditScroll.isHidden = false
}
}
}
This also preserved the widow's visual origin, and sizing minimum.
I have what should be a fairly simple question here. Basically I'm trying to move an object (a UIImageView) from a point A (where it is set in the storyboard) to a point B which I define programatically.
My first pass through this resulted in this code
UIView.animateWithDuration(0.75, delay: 0.5, options: UIViewAnimationOptions.CurveLinear, animations: {
self.signInButton.alpha = 1
self.signInButton.center.y = 10
}, completion: nil)
However, what this code does is to basically move the button offcenter and then back to its original location.
I then looked into QuartzCore to help me, but everything is in Objective-C. I have this method:
func moveImage(view: UIImageView){
var toPoint: CGPoint = CGPointMake(0.0, -10.0)
var fromPoint : CGPoint = CGPointZero
var movement = CABasicAnimation(keyPath: "movement")
movement.additive = true
movement.fromValue = fromPoint
movement.toValue = toPoint
movement.duration = 0.3
view.layer.addAnimation(movement, forKey: "move")
}
However the problem here is that movement.fromValue cannot accept a CGPoint. I know that in objective C there was a function that converted a CGPoint to a NSValue, however this function seems to be deprecated out of Swift and I can't find the other way of doing this.
Therefore my question is either, how do I convert CGPoint to NSValue to make my moveImage() function work, or is there a better way to move an object from point A to point B?
Thanks!
I'm looking at this question Animate UIImage in UIImageView Up & Down (Like it's hovering) Loop
Use NSValue(CGPoint: cgpiont) instead of NSValue.valueWithCGPoint(<#point: CGPoint#>) which is deprecated in swift. NSValue(CGPoint: cgpiont) is constructor given for that which can be used to convert CGPoint to NSValue
in swift.Following code will work
func moveImage(view: UIImageView){
var toPoint: CGPoint = CGPointMake(0.0, -10.0)
var fromPoint : CGPoint = CGPointZero
var movement = CABasicAnimation(keyPath: "movement")
movement.additive = true
movement.fromValue = NSValue(CGPoint: fromPoint)
movement.toValue = NSValue(CGPoint: toPoint)
movement.duration = 0.3
view.layer.addAnimation(movement, forKey: "move")
}
SWIFT 3 UPDATES
func moveImageView(imgView: UIImageView){
var toPoint:CGPoint = CGPoint(x: 0.0, y: -10.0)
var fromPoint:CGPoint = CGPoint.zero
var movement = CABasicAnimation(keyPath: "movement")
movement.isAdditive = true
movement.fromValue = NSValue(cgPoint: fromPoint)
movement.toValue = NSValue(cgPoint: toPoint)
movement.duration = 0.3
imgView.layer.add(movement, forKey: "move")
}