Skip to content

Commit

Permalink
Update cell size
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyu committed Aug 16, 2022
1 parent 0ba9291 commit c4bf068
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,8 @@ extension SelectedPhotoInputItemsViewController {
return size
} else {
let height: CGFloat = 160
let width: CGFloat
let ratio = CGFloat(asset.pixelWidth) / CGFloat(asset.pixelHeight)
if ratio > 1 {
width = ceil(height / 3 * 4)
} else if ratio < 1 {
width = ceil(height / 4 * 3)
} else {
width = height
}
let size = CGSize(width: width, height: height)
let width: CGFloat = ceil(height / CGFloat(asset.pixelHeight) * CGFloat(asset.pixelWidth))
let size = CGSize(width: min(160, max(width, 62)), height: height)
cellSizeCache[asset.localIdentifier] = size
return size
}
Expand Down
7 changes: 2 additions & 5 deletions Mixin/UserInterface/Windows/Cells/MediaPreviewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import MixinServices

class MediaPreviewCell: UICollectionViewCell {

static let cellSize = CGSize(width: 312, height: 312)

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var selectedStatusImageView: UIImageView!
@IBOutlet weak var mediaTypeView: MediaTypeOverlayView!
Expand Down Expand Up @@ -39,7 +37,7 @@ class MediaPreviewCell: UICollectionViewCell {
}
}

func load(asset: PHAsset) {
func load(asset: PHAsset, size: CGSize) {
if asset.mediaType == .video {
mediaTypeView.style = .video(duration: asset.duration)
} else {
Expand All @@ -49,8 +47,7 @@ class MediaPreviewCell: UICollectionViewCell {
mediaTypeView.style = .hidden
}
}
let targetSize = Self.cellSize * UIScreen.main.scale
requestId = PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: imageRequestOptions) { [weak self] (image, info) in
requestId = PHImageManager.default().requestImage(for: asset, targetSize: size * UIScreen.main.scale, contentMode: .aspectFill, options: imageRequestOptions) { [weak self] (image, info) in
self?.imageView.image = image
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class SelectedPhotoInputItemsPreviewWindow: BottomSheetView {
private var selectedAssets = [PHAsset]()
private var lastWidth: CGFloat = 0
private var isSending = false
private var cellSizeCache = [String: CGSize]()

override func awakeFromNib() {
super.awakeFromNib()
Expand Down Expand Up @@ -96,7 +97,7 @@ extension SelectedPhotoInputItemsPreviewWindow: UICollectionViewDataSource {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.media_preview, for: indexPath)!
if indexPath.item < assets.count {
let asset = assets[indexPath.item]
cell.load(asset: asset)
cell.load(asset: asset, size: cellSizeForItemAt(indexPath.item))
cell.updateSelectedStatus(isSelected: selectedAssets.contains(asset))
}
return cell
Expand All @@ -107,7 +108,7 @@ extension SelectedPhotoInputItemsPreviewWindow: UICollectionViewDataSource {
extension SelectedPhotoInputItemsPreviewWindow: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
MediaPreviewCell.cellSize
cellSizeForItemAt(indexPath.item)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
Expand Down Expand Up @@ -178,4 +179,20 @@ extension SelectedPhotoInputItemsPreviewWindow {
sendFileButton.isEnabled = isEnabled
}

private func cellSizeForItemAt(_ index: Int) -> CGSize {
guard index < assets.count else {
return .zero
}
let asset = assets[index]
if let size = cellSizeCache[asset.localIdentifier] {
return size
} else {
let height: CGFloat = 312
let width: CGFloat = ceil(height / CGFloat(asset.pixelHeight) * CGFloat(asset.pixelWidth))
let size = CGSize(width: min(312, max(width, 120)), height: height)
cellSizeCache[asset.localIdentifier] = size
return size
}
}

}

0 comments on commit c4bf068

Please sign in to comment.