如何从函数swift返回UIImage ?

时间:2021-04-09 16:04:36

I am downloading an image (successfully) from Parse. My question is can I place the function that is getting the image in a separate class and just get the UIImage as the returned parameter? See example below.

我正在从Parse下载一个图像(成功地)。我的问题是,我能否将获取图像的函数放在一个单独的类中,并将UIImage作为返回的参数?请参见下面的例子。

In both the return statements in the function below I am getting this error:

在下面函数的返回语句中,我得到了这个错误:

如何从函数swift返回UIImage ?

func getImgfromParse() -> UIImage {

    //var imgFile: PFFile = PFFile()
    var query = PFQuery(className: "puzzledata")
    query.whereKey("wordAnswer", equalTo: "asd")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {
            println("Found something")
            for object in objects {
                println(object.objectId)

                let pointsPerTile = object["pointsPerTile"] as String
                let timeAllowed = object["timeAllowed"] as String
                let wordAnswer = object["wordAnswer"] as String
                let wordJumbled = object["wordJumbled"] as String

                let imgFile = object["imageFile"] as PFFile


                imgFile.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                        let image = UIImage(data:imageData)
                        return image
                    }

                })//getDataInBackgroundWithBlock - end

            }
        }
        else {
            println("%@", error)
        }

        return UIImage(data: "profile.jpg")
    }
}

2 个解决方案

#1


2  

I think the problem is in your block

我想问题出在你那块

imgFile.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                        let image = UIImage(data:imageData)
                        return image
                    }

                })//getDataInBackgroundWithBlock - end

as you can see that the block provides two things imageData and error and expects you to return nothing i.e void but you are returning image from here, that's why it show's the error as 'cannot convert the expressions type UIImage to type void'.

正如您所看到的,该块提供了两项内容:imageData和error,并期望您不返回任何i。e void,但是你从这里返回图像,这就是为什么它显示了错误,因为“不能将表达式类型的UIImage转换为void”。

I think rather than returning image, you should call a function with image as the parameter and save that image there/ do your stuff with the image as

我认为与其返回图像,不如调用一个以图像为参数的函数,然后将图像保存在那里

imgFile.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                        let image = UIImage(data:imageData)
                        saveImage(image)
                    }

                })//getDataInBackgroundWithBlock - end

#2


0  

Your method is currect , I used same like this , But here working fine .. you can also this ..

你的方法很简单,我也是这么用的,不过在这里用得还可以。你也可以……

    func imageResize (imageObj:UIImage, sizeChange:CGSize)-> UIImage{

     let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    return scaledImage
}

Answer source : - http://www.jogendra.com/2014/11/image-resize-in-swift-ios8.html

答案来源:http://www.jogendra.com/2014/11/image-resize-in-swift-ios8.html

Please see this answer everything working fine

请看到这个答案一切正常

#1


2  

I think the problem is in your block

我想问题出在你那块

imgFile.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                        let image = UIImage(data:imageData)
                        return image
                    }

                })//getDataInBackgroundWithBlock - end

as you can see that the block provides two things imageData and error and expects you to return nothing i.e void but you are returning image from here, that's why it show's the error as 'cannot convert the expressions type UIImage to type void'.

正如您所看到的,该块提供了两项内容:imageData和error,并期望您不返回任何i。e void,但是你从这里返回图像,这就是为什么它显示了错误,因为“不能将表达式类型的UIImage转换为void”。

I think rather than returning image, you should call a function with image as the parameter and save that image there/ do your stuff with the image as

我认为与其返回图像,不如调用一个以图像为参数的函数,然后将图像保存在那里

imgFile.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                        let image = UIImage(data:imageData)
                        saveImage(image)
                    }

                })//getDataInBackgroundWithBlock - end

#2


0  

Your method is currect , I used same like this , But here working fine .. you can also this ..

你的方法很简单,我也是这么用的,不过在这里用得还可以。你也可以……

    func imageResize (imageObj:UIImage, sizeChange:CGSize)-> UIImage{

     let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    return scaledImage
}

Answer source : - http://www.jogendra.com/2014/11/image-resize-in-swift-ios8.html

答案来源:http://www.jogendra.com/2014/11/image-resize-in-swift-ios8.html

Please see this answer everything working fine

请看到这个答案一切正常