You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.6 KiB

package com.gyf.csams.util
import android.content.Context
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.core.graphics.drawable.toBitmap
import coil.imageLoader
import coil.request.ErrorResult
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.orhanobut.logger.Logger
class ImageUtil {
companion object {
/**
*
*
* @param data Set the data to load.
* The default supported data types are:
* String (mapped to a Uri)
* Uri ("android.resource", "content", "file", "http", and "https" schemes only)
* HttpUrl
* File
* DrawableRes
* Drawable
* Bitmap
*/
suspend fun getImage(context: Context, data: Any?): ImageBitmap? {
val request = ImageRequest.Builder(context)
.data(data = data)
.build()
return when (val result = context.imageLoader.execute(request)) {
is SuccessResult -> result.drawable.toBitmap().asImageBitmap()
is ErrorResult -> {
val drawable = result.drawable
return if (drawable == null) {
Logger.e("${data}图片加载失败")
null
} else {
drawable.toBitmap().asImageBitmap()
}
}
}
}
}
}