Update alpha of image inside UICollectionViewCell

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Update alpha of image inside UICollectionViewCell



I have a list of an object that have image inside it, I upload the image in this object to firebase storage after showing it inside a collectionviewcell, every thing is work correctly but I need change the alpha of image inside the uicollectionviewcell according to progress of uploading it to firebase storage, what I try the following:


class MediaItem :Codable
{
var id : Int? = 0
var url : String? = ""
var pickedImage = UIImage()
var currentState:State = .none
var progress:Float = 0

func start () {
currentState = .uploading
var data = Data()
data = UIImageJPEGRepresentation(self.pickedImage, 0.8)!
// Create a root reference
let storageRef = Storage.storage().reference()
let imageName = NSUUID().uuidString
// Create a reference to the file you want to upload
let imageRef = storageRef.child("images/(imageName).jpg")

// Upload the file to the path "images/
let uploadTask = imageRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
}
_ = uploadTask.observe(.progress) { snapshot in
self.progress = (Float(100.0 * Double(snapshot.progress!.completedUnitCount)
/ Double(snapshot.progress!.totalUnitCount))/100) //Float(snapshot.progress!.completedUnitCount)
print("self.progress",self.progress)
}
uploadTask.observe(.success) { snapshot in
self.currentState = .uploaded
self.url = imageName
}
uploadTask.observe(.failure) { snapshot in
self.currentState = .none
print("Failuer")
}
}
}



When pick an image:


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
dismiss(animated: true, completion:nil)
let tempMediaItem = MediaItem()

if let pickedImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)
{
tempMediaItem.pickedImage = pickedImage
}

else if let videoUrl = info[UIImagePickerControllerMediaURL] as? NSURL{
print("videourl: ", videoUrl)
//trying compression of video
// let data = NSData(contentsOf: videoUrl as URL)!
tempMediaItem.pickedImage = firstFrame(videoUrl as URL)!
}
else
{
print("Something went wrong")
return
}

tempMediaItem.start()
arrayOfImages?.append(tempMediaItem)
collView.reloadData()
}



And in UICollectionView, cellForItemAt indexPath :


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "editPostCell", for: indexPath) as! editPostCell

cell.workImage.image = arrayOfImages?[indexPath.row].pickedImage


// cell.workImage.alpha = 0.1
if arrayOfImages?[indexPath.row].currentState == .none
{
arrayOfImages?[indexPath.row].start()
}
cell.workImage.alpha = CGFloat((arrayOfImages?[indexPath.row].progress)!)
return cell
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions