我正在使用 Photos Framework 填充 UICollectionView。这应该是一个简单的模式,但我在取消对隐藏单元格的请求时遇到了问题。
这是我的代码,取自几个例子:
#pragma mark - UICollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_photos count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@">>>>>>SHOWING CELL %d WITH MEMADDRESS %p", indexPath.row, cell);
PHAsset *asset = [_photos objectAtIndex:indexPath.row];
PHImageRequestOptions *option = [PHImageRequestOptions new];
option.synchronous = NO;
option.version = PHImageRequestOptionsVersionCurrent;
option.networkAccessAllowed = YES;
option.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
option.resizeMode = PHImageRequestOptionsResizeModeFast;
PHImageRequestID requestId = cell.tag;
if(requestId != 0)
{
[_imgManager cancelImageRequest:requestId];
NSLog(@">>>>>>1ST CANCEL CELL %d WITH MEMADDRESS %p AND REQUESTID %d", indexPath.row, cell, requestId);
}
[cell.photoIV setImage:nil];
cell.tag = [_imgManager requestImageForAsset:asset
targetSize:CGSizeMake(CAMERA_GALLERY_SIZE, CAMERA_GALLERY_SIZE)
// contentMode:PHImageContentModeAspectFit
contentMode:PHImageContentModeDefault
options:option
resultHandler:^(UIImage *result, NSDictionary *info) {
//if([self isRowVisible:indexPath])
//{
[cell.photoIV setImage:result];
NSLog(@">>>>>>Assigning photo to cell %d with memaddress %p", indexPath.row, cell);
//}
//else
//{
// NSLog(@">>>>>>We should assign photo to cell %d with memaddress %p but this cell is not visible", indexPath.row, cell);
//}
}];
NSLog(@">>>>>>LOADING CELL %d WITH MEMADDRESS %p AND REQUESTID %d", indexPath.row, cell, requestId);
if([_selectedPhotos containsObject:asset])
[cell.selectedIV setHidden:NO];
else
[cell.selectedIV setHidden:YES];
[cell.isMineIV setHidden:YES];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>>>>>HIDDING CELL %d WITH MEMADDRESS %p", indexPath.row, cell);
[((GalleryViewCell *)cell).photoIV setImage:nil];
PHImageRequestID requestId = cell.tag;
if(requestId != 0)
{
[_imgManager cancelImageRequest:requestId];
NSLog(@">>>>>>2ND CANCEL CELL %d WITH MEMADDRESS %p AND REQUESTID %d", indexPath.row, cell, requestId);
}
}
-(BOOL) isRowVisible:(NSIndexPath *) indexPath
{
NSArray *indexes = [_photosCV indexPathsForVisibleItems];
for (NSIndexPath *index in indexes)
{
if (index.row == indexPath.row)
{
return YES;
}
}
return NO;
}
好吧,如果我在 Collection View 中快速上下滚动,每个单元格都会刷新几次,在 1 或 2 秒内显示 2 甚至 3 张图像。 当单元格不再可见或单元格再次可见时,我试图取消同一单元格中的先前请求,但查看结果并查看日志似乎没有请求被取消。
这些是我向下滚动和向上滚动到列表顶部后的日志。
2016-10-24 22:10:46.284 Fotoloop[9834:4281410] >>>>>>HIDDING CELL 17 WITH MEMADDRESS 0x17351660
2016-10-24 22:10:46.286 Fotoloop[9834:4281410] >>>>>>2ND CANCEL CELL 17 WITH MEMADDRESS 0x17351660 AND REQUESTID 351
2016-10-24 22:10:46.288 Fotoloop[9834:4281410] >>>>>>HIDDING CELL 15 WITH MEMADDRESS 0x1736a890
2016-10-24 22:10:46.289 Fotoloop[9834:4281410] >>>>>>2ND CANCEL CELL 15 WITH MEMADDRESS 0x1736a890 AND REQUESTID 349
2016-10-24 22:10:46.291 Fotoloop[9834:4281410] >>>>>>SHOWING CELL 0 WITH MEMADDRESS 0x1736a890
2016-10-24 22:10:46.291 Fotoloop[9834:4281410] >>>>>>1ST CANCEL CELL 0 WITH MEMADDRESS 0x1736a890 AND REQUESTID 349
2016-10-24 22:10:46.293 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 0 with memaddress 0x1736a890
2016-10-24 22:10:46.293 Fotoloop[9834:4281410] >>>>>>LOADING CELL 0 WITH MEMADDRESS 0x1736a890 AND REQUESTID 349
2016-10-24 22:10:46.298 Fotoloop[9834:4281410] >>>>>>SHOWING CELL 1 WITH MEMADDRESS 0x17351660
2016-10-24 22:10:46.298 Fotoloop[9834:4281410] >>>>>>1ST CANCEL CELL 1 WITH MEMADDRESS 0x17351660 AND REQUESTID 351
2016-10-24 22:10:46.301 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 1 with memaddress 0x17351660
2016-10-24 22:10:46.303 Fotoloop[9834:4281410] >>>>>>LOADING CELL 1 WITH MEMADDRESS 0x17351660 AND REQUESTID 351
2016-10-24 22:10:46.310 Fotoloop[9834:4281410] >>>>>>SHOWING CELL 2 WITH MEMADDRESS 0x1744d350
2016-10-24 22:10:46.311 Fotoloop[9834:4281410] >>>>>>1ST CANCEL CELL 2 WITH MEMADDRESS 0x1744d350 AND REQUESTID 350
2016-10-24 22:10:46.312 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 2 with memaddress 0x1744d350
2016-10-24 22:10:46.312 Fotoloop[9834:4281410] >>>>>>LOADING CELL 2 WITH MEMADDRESS 0x1744d350 AND REQUESTID 350
2016-10-24 22:10:46.316 Fotoloop[9834:4281410] >>>>>>SHOWING CELL 3 WITH MEMADDRESS 0x171c0fd0
2016-10-24 22:10:46.317 Fotoloop[9834:4281410] >>>>>>1ST CANCEL CELL 3 WITH MEMADDRESS 0x171c0fd0 AND REQUESTID 348
2016-10-24 22:10:46.318 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 3 with memaddress 0x171c0fd0
2016-10-24 22:10:46.318 Fotoloop[9834:4281410] >>>>>>LOADING CELL 3 WITH MEMADDRESS 0x171c0fd0 AND REQUESTID 348
2016-10-24 22:10:46.325 Fotoloop[9834:4281410] >>>>>>SHOWING CELL 4 WITH MEMADDRESS 0x15f15b60
2016-10-24 22:10:46.339 Fotoloop[9834:4281410] >>>>>>1ST CANCEL CELL 4 WITH MEMADDRESS 0x15f15b60 AND REQUESTID 347
2016-10-24 22:10:46.363 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 4 with memaddress 0x15f15b60
2016-10-24 22:10:46.365 Fotoloop[9834:4281410] >>>>>>LOADING CELL 4 WITH MEMADDRESS 0x15f15b60 AND REQUESTID 347
2016-10-24 22:10:46.371 Fotoloop[9834:4281410] >>>>>>SHOWING CELL 5 WITH MEMADDRESS 0x1710b1b0
2016-10-24 22:10:46.373 Fotoloop[9834:4281410] >>>>>>1ST CANCEL CELL 5 WITH MEMADDRESS 0x1710b1b0 AND REQUESTID 346
2016-10-24 22:10:46.376 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 5 with memaddress 0x1710b1b0
2016-10-24 22:10:46.377 Fotoloop[9834:4281410] >>>>>>LOADING CELL 5 WITH MEMADDRESS 0x1710b1b0 AND REQUESTID 346
2016-10-24 22:10:46.399 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 23 with memaddress 0x171d3600
2016-10-24 22:10:46.401 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 16 with memaddress 0x1744d350
2016-10-24 22:10:46.408 Fotoloop[9834:4281410] >>>>>>HIDDING CELL 12 WITH MEMADDRESS 0x15feda90
2016-10-24 22:10:46.408 Fotoloop[9834:4281410] >>>>>>2ND CANCEL CELL 12 WITH MEMADDRESS 0x15feda90 AND REQUESTID 352
2016-10-24 22:10:46.409 Fotoloop[9834:4281410] >>>>>>HIDDING CELL 13 WITH MEMADDRESS 0x171d7d00
2016-10-24 22:10:46.409 Fotoloop[9834:4281410] >>>>>>2ND CANCEL CELL 13 WITH MEMADDRESS 0x171d7d00 AND REQUESTID 353
2016-10-24 22:10:46.410 Fotoloop[9834:4281410] >>>>>>HIDDING CELL 14 WITH MEMADDRESS 0x171ef8d0
2016-10-24 22:10:46.410 Fotoloop[9834:4281410] >>>>>>2ND CANCEL CELL 14 WITH MEMADDRESS 0x171ef8d0 AND REQUESTID 354
2016-10-24 22:10:46.430 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 19 with memaddress 0x15f15b60
2016-10-24 22:10:46.490 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 15 with memaddress 0x1736a890
2016-10-24 22:10:46.504 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 0 with memaddress 0x1736a890
2016-10-24 22:10:46.551 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 17 with memaddress 0x17351660
2016-10-24 22:10:46.614 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 13 with memaddress 0x171d7d00
2016-10-24 22:10:46.677 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 9 with memaddress 0x1735b730
2016-10-24 22:10:46.743 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 12 with memaddress 0x15feda90
2016-10-24 22:10:46.808 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 10 with memaddress 0x171e3660
2016-10-24 22:10:46.865 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 14 with memaddress 0x171ef8d0
2016-10-24 22:10:46.927 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 11 with memaddress 0x15f27020
2016-10-24 22:10:46.996 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 6 with memaddress 0x171d3600
2016-10-24 22:10:47.053 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 7 with memaddress 0x17133010
2016-10-24 22:10:47.088 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 3 with memaddress 0x171c0fd0
2016-10-24 22:10:47.114 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 4 with memaddress 0x15f15b60
2016-10-24 22:10:47.118 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 2 with memaddress 0x1744d350
2016-10-24 22:10:47.122 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 1 with memaddress 0x17351660
2016-10-24 22:10:47.125 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 8 with memaddress 0x15f06e30
2016-10-24 22:10:47.213 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 5 with memaddress 0x1710b1b0
如您所见,单元格 17 (0x17351660) 已隐藏,我取消了它的请求。
2016-10-24 22:10:46.284 Fotoloop[9834:4281410] >>>>>>HIDDING CELL 17 WITH MEMADDRESS 0x17351660
2016-10-24 22:10:46.286 Fotoloop[9834:4281410] >>>>>>2ND CANCEL CELL 17 WITH MEMADDRESS 0x17351660 AND REQUESTID 351
紧接着,单元格 1 (0x17351660) 显示出来,我为此单元格发起了一个新请求。
2016-10-24 22:10:46.298 Fotoloop[9834:4281410] >>>>>>SHOWING CELL 1 WITH MEMADDRESS 0x17351660
2016-10-24 22:10:46.298 Fotoloop[9834:4281410] >>>>>>1ST CANCEL CELL 1 WITH MEMADDRESS 0x17351660 AND REQUESTID 351
2016-10-24 22:10:46.301 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 1 with memaddress 0x17351660
2016-10-24 22:10:46.303 Fotoloop[9834:4281410] >>>>>>LOADING CELL 1 WITH MEMADDRESS 0x17351660 AND REQUESTID 351
但一段时间后我得到了这个:
2016-10-24 22:10:46.551 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 17 with memaddress 0x17351660
2016-10-24 22:10:47.122 Fotoloop[9834:4281410] >>>>>>Assigning photo to cell 1 with memaddress 0x17351660
我可以在一秒钟内看到该单元格中显示的 2 张图像。 如果我取消注释 isRowVisible block ,我看不到这种丑陋的效果,但我一直认为我的代码取消请求存在问题,这可能导致大量照片的性能不佳。 我在这里缺少什么?
最佳答案
我正在将 Photos 实现到 UICollectionView 中,并发现了与您相同的结果。起初,我认为 cancelImageRequest 不起作用,因为当快速滚动时,性能会受到影响,我会看到 2-3 个图像在一个单元格中闪烁,直到最终落在正确的图像上。我还注意到 PHImageCancelledKey 永远不会存在于 resultHandler 信息字典中,即使我在 collectionView didEndDisplaying 上取消也是如此。
但是,我确实解决了我现在要解释的问题。因为 PHImageManager requestImage 是异步的,这很可能在后台队列上运行,这在执行任何与 UI 相关的操作时可能会导致奇怪的结果。在 resultHandler 中,确保处理主队列中的所有内容,它应该可以解决您看到的问题。至少它对我有用。
我正在 iPhone 5s 上滚动浏览 800 张图片进行测试。
这是我当前测试的 collectionView 代码示例。
我还在下面提供了加载照片获取结果并缓存它们的位置。我使用的 self.imageManager 是 PHCachingImageManager。
只想补充一点,您会注意到我对 wasCancelled 和 isSameRequest 进行了 bool 检查。这些是无关紧要的,因为取消的 key 永远不存在,现在所有内容都在主队列上处理,图像闪烁不再发生。我把那些留在那里,以防你想自己看看。
// MARK: UICollectionViewDelegate
public func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if let assetFetchResult = self.assetFetchResult
{
return assetFetchResult.count
}
return 0
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell: PhotoLibraryCell = self.itemsCollectionView.dequeueReusableCell(withReuseIdentifier: PhotoLibraryCell.ReuseIdentifier, for: indexPath) as! PhotoLibraryCell
cell.backgroundColor = .white
cell.drawBorder(color: .lightGray, borderWidth: 1)
cell.imageView.image = nil
if let assetFetchResult = self.assetFetchResult
{
let asset: PHAsset = assetFetchResult.object(at: indexPath.row)
let itemSize: CGSize = Settings.ItemSize
let targetSize: CGSize = CGSize(width: itemSize.width * 2, height: itemSize.height * 2)
if (asset.mediaType == .image)
{
cell.imageRequestID = self.imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: nil, resultHandler: { (image: UIImage?, info: [AnyHashable: Any]?) in
DispatchQueue.main.async {
var isDegraded: Bool = false
var wasCancelled: Bool = false
var isSameRequest: Bool = false
if let degraded = (info?[PHImageResultIsDegradedKey] as? NSNumber)?.boolValue
{
isDegraded = degraded
}
if let cancelled = (info?[PHImageCancelledKey] as? NSNumber)?.boolValue
{
wasCancelled = cancelled
}
if let id = info?[PHImageResultRequestIDKey] as? NSNumber, let cellRequestID = cell.imageRequestID
{
let requestID: Int = id.intValue
isSameRequest = Int(cellRequestID) == requestID
}
if (!isDegraded && !wasCancelled && isSameRequest)
{
cell.imageView.image = image
}
}
})
}
else if (asset.mediaType == .video)
{
}
}
return cell
}
public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
{
if let photoLibraryCell = cell as? PhotoLibraryCell
{
if let imageRequestID = photoLibraryCell.imageRequestID
{
self.imageManager.cancelImageRequest(imageRequestID)
}
}
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return Settings.ItemSize
}
// MARK: Assets
public func reloadAssets()
{
let photoResult: PHFetchResult<PHAsset> = PHAsset.fetchAssets(with: .image, options: nil)
self.assetFetchResult = photoResult
self.itemsCollectionView.reloadData()
//enumerate assets and start caching them
let backgroundQueue: DispatchQueue = DispatchQueue(label: "enumerate-phassets-background-queue", qos: .background, target: nil)
backgroundQueue.async {
var assets: [PHAsset] = Array()
let itemSize: CGSize = Settings.ItemSize
let targetSize: CGSize = CGSize(width: itemSize.width * 2, height: itemSize.height * 2)
photoResult.enumerateObjects({ (asset: PHAsset, index: Int, stopEnumeration: UnsafeMutablePointer<ObjCBool>) -> Void in
assets.append(asset)
let reachedEnd: Bool = index == photoResult.count - 1
if (reachedEnd)
{
self.imageManager.startCachingImages(for: assets, targetSize: targetSize, contentMode: .aspectFill, options: nil)
}
})
}
}
关于iOS PHImageManager cancelImageRequest 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40226949/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
使用Ruby1.9.2运行IDE提示说需要gemruby-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall
我知道全局变量$!包含最新的异常对象,但我对下面的语法感到困惑。谁能帮助我理解以下语法?rescue$! 最佳答案 此构造可防止异常停止您的程序并使堆栈跟踪冒泡。它还会将该异常作为值返回,这很有用。a=get_me_datarescue$!在此行之后,a将保存请求的数据或异常。然后您可以分析该异常并采取相应措施。defget_me_dataraise'Nodataforyou'enda=get_me_datarescue$!puts"Executioncarrieson"pa#>>Executioncarrieson#>>#更现实的
我在我正在处理的一些代码中发现了这一点。它旨在解决从磁盘读取key文件的要求。在生产环境中,key文件的内容位于环境变量中。旧代码:key=File.read('path/to/key.pem')新代码:key=File.read('|echo$KEY_VARIABLE')这是如何工作的? 最佳答案 来自IOdocs:Astringstartingwith“|”indicatesasubprocess.Theremainderofthestringfollowingthe“|”isinvokedasaprocesswithappro
我今天看到了一个ruby代码片段。[1,2,3,4,5,6,7].inject(:+)=>28[1,2,3,4,5,6,7].inject(:*)=>5040这里的注入(inject)和之前看到的完全不一样,比如[1,2,3,4,5,6,7].inject{|sum,x|sum+x}请解释一下它是如何工作的? 最佳答案 没有魔法,符号(方法)只是可能的参数之一。这是来自文档:#enum.inject(initial,sym)=>obj#enum.inject(sym)=>obj#enum.inject(initial){|mem
我刚刚有一个关于RubyonRails和模型(Rails3)中的attr_accessible属性的一般性问题。有人可以解释应该在那里定义哪些模型属性吗?我记得一些关于批量分配风险的事情,虽然我在这方面不太了解......谢谢:) 最佳答案 想象一个带有一些字段的订单类:Order.new({:type=>'Corn',:quantity=>6})现在假设订单也有折扣代码,比如:price_off。您不想将:price_off标记为attr_accessible。这会阻止恶意代码制作最终会执行如下操作的帖子:Order.new({: