나는 e를 사용하여 UICollectionView
에 이미지를로드합니다. 그런 다음 사용자가 이미지를 클릭하면 사본을 Pasteboard
에 복사하여 다른 앱에 붙여 넣을 수 있습니다.SDWebImage를 사용하여 NSData로 이미지를 붙여 넣기로 복사
현재 didSelectItemAtIndexPath
방법에서 사용중인 코드는 웹으로 돌아가 이미지를 복사합니다. 그러나 이미지는 이미 사용자의 캐시에 있어야합니다.
을 사용하여 NSData
의 이미지를 가져 오는 방법은 무엇입니까?
그런 식으로 앱이 먼저 이미지 캐시를 확인합니다. SDWebImage
을 사용하려고하면 DataType
문제가 계속 발생합니다.
이것은 현재 코드입니다. 나는 didSelectItemAtIndexPath
의 코드를 수정하려는 :
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let iCell = collectionView.dequeueReusableCellWithReuseIdentifier("ImageCollectionViewCell", forIndexPath: indexPath) as! ImageCollectionViewCell
let url = NSURL(string: self.imgArray.objectAtIndex(indexPath.row) as! String)
iCell.imgView.sd_setImageWithURL(url)
return iCell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// Change code below to use SDWebImage
let url = NSURL(string: self.imgArray.objectAtIndex(indexPath.row) as! String)
let data = NSData(contentsOfURL: url!)
if data != nil {
UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: kUTTypePNG as String)
} else {
// Do nothing
}
}
시도해보십시오. 'let image = SDImageCache.sharedImageCache.imageFromDiskCacheForKey (url!); 데이터 = UIImagePNGRepresentation (이미지)하자'? 'image'가 있는지 (이미지가 이미 캐시에 있음) 확인해야 할 수도 있습니다. – Larme
감사합니다. 이것은 효과가 있었다. 그리고 당신은 정확합니다, 나는 이미지가 존재하는지 확인해야합니다. 그렇지 않으면 거기에 없으면 충돌합니다. if 문을 추가하여 'let data = UIImagePNGRepresentation (image)'이전에 'image! = nil'을 확인하고 원본 코드로 온라인 이미지를 찾습니다. –