0

I am developing an IOS app using Swift and Firebase. I want to generate a Barcode for each user and provide discounts to them. How can I generate a Barcode? Which Barcode scanning device is suitable?

I have googled and read, but they are not useful. Please shed some light and provide any useful link or website.

I have tried below code, but it's not generating an Barcode. I don't see any output/Barcode image. My screen was empty.

@IBAction func gen(_ sender: UIButton) {
    imageDisplay.image = generateBarcodeFromString(from: tfInput.text!)

}




/*     imageDisplay.image = generateBarcodeFromString(from: tfInput.text!)

*/

func generateBarcodeFromString(from string: String)-> UIImage?{

let data = string.data(using: String.Encoding.ascii)

let filter = CIFilter(name: "CICode128BarcodeGenerator")

 filter?.setValue(data, forKey: "inputMessage")

    let transform = __CGAffineTransformMake(10, 10, 10, 10, 10, 10) /*          CGAffineTransform(scaleX: 3, y: 3)

*/

    if let output = filter?.outputImage?.applying(transform){

        return UIImage(ciImage: output)

    }

    return nil

}
Sourabh
  • 39
  • 1
  • 7
  • 1
    what you tried till now? – Shabir jan May 12 '17 at 12:29
  • I tried that duplicate post, but its not working or not useful. as it is 2 years old. – Sourabh May 12 '17 at 12:36
  • 1
    Then you should either edit your question or ask a new question, showing what you tried and the specific problems you are experiencing. – Paulw11 May 12 '17 at 12:38
  • it's better I will edit it. – Sourabh May 12 '17 at 12:43
  • @Paulw11 I have edited the question. – Sourabh May 12 '17 at 12:46
  • Neither of those blocks of code will add anything to the screen. They will return a `UIImage` that you could put in a `UIImageView`. Did they return a non-nil value? – Paulw11 May 12 '17 at 12:50
  • @Paulw11 I have tried this code, but I got an error. When I am returning nil, error is "Nil is not compatible with return type 'UIImage' ". – Sourabh May 12 '17 at 14:20
  • "func generateBarcodeFromString(from string: String)-> UIImage{ let data = string.data(using: String.Encoding.ascii) let filter = CIFilter(name: "CICode128BarcodeGenerator") filter?.setValue(data, forKey: "inputMessage") let transform = CGAffineTransform(scaleX: 3, y: 3) /* __CGAffineTransformMake(10, 10, 10, 10, 10, 10) */ if let output = filter?.outputImage?.applying(transform){ return UIImage(ciImage: output) } return nil }" – Sourabh May 12 '17 at 14:20
  • Please suggest. – Sourabh May 12 '17 at 14:21
  • The error message is quite clear if you know the basics of Swift: either make the return type an optional, or don't return nil. My advice is to make the function return `UIImage?` instead of `UIImage`... and indeed this is what they do in the duplicate. – Eric Aya May 12 '17 at 16:32
  • @EricAya I tried returning UIImage but when I return UIImage I am getting an error "cannot convert return expression of type 'UIImage!.Type(aka 'ImplicitlyUnwrappedOptional.Type') to return type 'UIImage' " – Sourabh May 15 '17 at 15:05
  • @EricAya If I return nil, getting error "Nil is incompatible with return type 'UIImage' " – Sourabh May 15 '17 at 15:06
  • Please suggest. – Sourabh May 15 '17 at 15:06
  • Read my comment again... You're missing an important part. – Eric Aya May 15 '17 at 15:07
  • func generateBarcodeFromString(from string: String)-> UIImage?{ let data = string.data(using: String.Encoding.ascii) let filter = CIFilter(name: "CICode128BarcodeGenerator") filter?.setValue(data, forKey: "inputMessage") let transform = CGAffineTransform(scaleX: 3, y: 3) /* __CGAffineTransformMake(10, 10, 10, 10, 10, 10) */ if let output = filter?.outputImage?.applying(transform){ return UIImage(ciImage: output) } return nil } } – Sourabh May 15 '17 at 15:18
  • Thank you, I just cleared the error. – Sourabh May 15 '17 at 15:19
  • Though I cleared the error, barcode is not generating, please suggest. – Sourabh May 15 '17 at 20:57
  • After doing enough research I did not find any good document, So I have posted here. – Sourabh May 16 '17 at 09:20

0 Answers0