我对此代码有疑问
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
kLatestKivaLoansURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
Run Code Online (Sandbox Code Playgroud)
这段代码的第一个参数是
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
Run Code Online (Sandbox Code Playgroud)
我们是否要求此代码在全局队列上执行串行任务,其定义本身是返回给定优先级的全局并发队列?
使用dispatch_get_global_queue主队列有什么好处?
我很迷惑.你能帮我更好地理解这个吗?
ParseImageView是否在Android中缓存ParseFile.如果它缓存parseFile,我如何在我的Android设备中找到这些文件的路径.
ParseImageView imageView = (ParseImageView) findViewById(android.R.id.icon);
// The placeholder will be used before and during the fetch, to be replaced by the fetched image
// data.
imageView.setPlaceholder(getResources().getDrawable(R.drawable.placeholder));
imageView.setParseFile(file);
imageView.loadInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
Log.i("ParseImageView",
"Fetched! Data length: " + data.length + ", or exception: " + e.getMessage());
}
});
Run Code Online (Sandbox Code Playgroud) 背景
我搜索了SO和苹果论坛.相当多的人谈到了带图像的集合视图单元的性能.他们中的大多数人表示,由于在主线程中加载图像,因此滚动滞后.
通过使用SDWebImage,图像应该在单独的线程中加载.但是,它仅在iPad模拟器中的横向模式下滞后.
问题描述
在纵向模式下,集合视图为每行加载3个单元格.它没有滞后或微不足道的延迟.在横向模式下,集合视图为每行加载4个单元格.并且它具有明显的滞后和帧速率下降.
我已经使用带有核心动画的乐器工具进行了检查.出现新单元格时,帧速率降至约8fps.我不确定哪种行为会给集合视图带来如此低的性能.
希望有人知道这个技巧部分.
这是相关代码
在视图控制器中
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ProductCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ProductViewCell" forIndexPath:indexPath];
Product *tmpProduct = (Product*)_ploader.loadedProduct[indexPath.row];
cell.product = tmpProduct;
if (cellShouldAnimate) {
cell.alpha = 0.0;
[UIView animateWithDuration:0.2
delay:0
options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
animations:^{
cell.alpha = 1.0;
} completion:nil];
}
if(indexPath.row >= _ploader.loadedProduct.count - ceil((LIMIT_COUNT * 0.3)))
{
[_ploader loadProductsWithCompleteBlock:^(NSError *error){
if (nil == error) {
cellShouldAnimate = NO;
[_collectionView reloadData];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
cellShouldAnimate = YES;
});
} …Run Code Online (Sandbox Code Playgroud) 我的 collectionViewCell 中有通过 NSURLRequest 获取和解析的图像,如何缓存这些图像,以便它们不必在视图的每次出现/消失时启动新请求?
这是我获取图像的代码:
class funnyPicture: NSObject {
var pfPicture : PFObject
var coverImage : UIImage!
init(pfPicture: PFObject) {
self.pfPicture = pfPicture
}
func fetchCoverImage(completion: (image: UIImage?, error: NSError?) -> Void) {
let urlString = self.pfPicture["funnyPictures"] as! String
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
let queue = dispatch_get_main_queue()
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
if error == nil {
self.coverImage = UIImage(data: data!)
completion(image: self.coverImage, error: nil)
} else …Run Code Online (Sandbox Code Playgroud)