最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - Convert UIImage.size to String - Stack Overflow

programmeradmin0浏览0评论
if(image != nil){
    mediaSize = String(image!.size.width) + "|" + String(image!.size.height)
}

The result should be like "400|300"

I get the error: No exact matches in call to initializer

image is UIImage and I'm uploading it to the server before I try to convert the size to string and it's valid

Other questions about this aren't helping

if(image != nil){
    mediaSize = String(image!.size.width) + "|" + String(image!.size.height)
}

The result should be like "400|300"

I get the error: No exact matches in call to initializer

image is UIImage and I'm uploading it to the server before I try to convert the size to string and it's valid

Other questions about this aren't helping

Share Improve this question edited Nov 19, 2024 at 12:10 David Pasztor 54.9k9 gold badges97 silver badges127 bronze badges asked Nov 19, 2024 at 11:55 TheGreatCornholioTheGreatCornholio 1,5451 gold badge23 silver badges49 bronze badges 1
  • Sorry, it's UIImage – TheGreatCornholio Commented Nov 19, 2024 at 11:57
Add a comment  | 

3 Answers 3

Reset to default 1

The problem is that both height and width are CGFloat, which cannot be directly converted to a String using String.init.

You can either convert them to Double and then initialise a String from the Double

guard let image else { return }
let width = Double(image.size.width)
let height = Double(image.size.height)
mediaSize = String(width) + "|" + String(height)

Or simply use String interpolation, which does support CGFloat.

mediaSize = "\(image.size.width) | \(image.size.height)"

However, be aware that you're converting floating point numbers to a String, so you might end up with a non user friendly value with tons of decimal places. If you know that your values are integers, convert them to integers using Int(image.size.width) or format them to a fixed number of decimal places using either String(format:_:) or a NumberFormatter.

You can also use String(format:...):

var mediaSize: String = ""
let image = UIImage(named: "myImage")
if let image {
    mediaSize = String(format: "%0.0f|%0.0f", image.size.width, image.size.height)
    print(mediaSize)
}

The %0.0f means convert float to string, with NO decimals.

So your result would be "400|300"

Here’s a clean and reusable extension for converting an image size to a formatted string. This ensures your code is modular, readable, and easy to use across your project. UIImage Extension

import UIKit

extension UIImage {
    /// Returns the image size as a formatted string: "width|height".
    func formattedSizeString(separator: String = "|") -> String {
        return "\(size.width)\(separator)\(size.height)"
    }
}

How to Use It With this extension, you can directly call formattedSizeString() on any UIImage instance:

if let image = image {
    let mediaSize = image.formattedSizeString() // Default separator "|"
    print("Media size: \(mediaSize)")
}

// Using a custom separator
if let image = image {
    let mediaSize = image.formattedSizeString(separator: "x")
    print("Media size: \(mediaSize)") // Example: "1920x1080"
}

Key Features

  • Default Separator: The default separator is "|", but you can customize it when calling the method. Code Reusability:

  • By encapsulating the logic in an extension, you avoid repeating string formatting logic throughout your code. Readability:

  • Using a named method (formattedSizeString) makes it clear what the function does.

发布评论

评论列表(0)

  1. 暂无评论