首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode iOS应用程序挂在启动屏幕上"semaphore_wait_trap()“

Xcode iOS应用程序挂在启动屏幕上"semaphore_wait_trap()“
EN

Stack Overflow用户
提问于 2016-12-15 15:05:41
回答 1查看 457关注 0票数 1

对不起,我是个十足的菜鸟,不是程序员。我基于一个模板创建了一个照片编辑应用程序,并在谷歌搜索、教程等的帮助下对其进行了大量定制。

使用Xcode7.3.1,iOS 9.3,更新的Photosframework和唯一的objective C。我已经让应用程序达到了我对它很满意的程度,除了我注意到在第一次启动时,应用程序挂起(调试报告semaphore_wait_trap() )。

在iOS 9.3中,应用程序无法进入下一步“请求访问照片”提示,唯一的方法是点击主页按钮,然后看到授予访问警告,然后切换回应用程序。然后退出应用程序,重新加载,然后它就可以超时运行了。这当然不是理想的用户体验。

如果我暂停调试模式,它会挂起:"semaphore_wait_trap()“

我已经用谷歌搜索了几天,但找不到一个解决方案来让权限提示弹出窗口显示在我的应用程序窗口的顶部。

这超出了我的能力。任何想法都将不胜感激。

查看位于警报弹出窗口顶部的启动图像的屏幕截图。

如果您按下“主页”按钮,将出现允许访问照片的警告。

应用程序代理:

代码语言:javascript
复制
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
    // Sets icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}
//  END Local Notification ==========================


return true;

}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

// Resets icon's badge number to zero
application.applicationIconBadgeNumber = 0;

}

下面是主视图控制器的一个片段(希望它不会太长,不确定问题出在哪里)

HomeVC.m:

代码语言:javascript
复制
#import "HomeVC.h"
#import "Configs.h"
#import "AAPLGridViewCell2.h"
#import "NSIndexSet+Convenience.h"
#import "UICollectionView+Convenience.h"
#import "AAPLRootListViewController.h"
#import "Configs.h"
#import "ImageEditorTheme.h"
#import "ImageEditorTheme+Private.h"

@import PhotosUI;
@import UIKit;


@interface HomeVC()
<
PHPhotoLibraryChangeObserver,
UICollectionViewDelegateFlowLayout,
UICollectionViewDataSource,
UICollectionViewDelegate
>
@property (nonatomic, strong) NSArray *sectionFetchResults;
@property (nonatomic, strong) NSArray *sectionLocalizedTitles;
@property (nonatomic, strong) PHCachingImageManager *imageManager;
@property CGRect previousPreheatRect;
@property (nonatomic, strong) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, assign) CGSize lastTargetSize;


@end


@implementation HomeVC

{
    UIActivityIndicatorView *_indicatorView;
}

static NSString * const AllPhotosReuseIdentifier = @"AllPhotosCell";
static NSString * const CollectionCellReuseIdentifier = @"CollectionCell";

static NSString * const CellReuseIdentifier = @"Cell";
static CGSize AssetGridThumbnailSize;


- (void)awakeFromNib {

    self.imageManager = [[PHCachingImageManager alloc] init];
    [self resetCachedAssets];

    [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];

}

- (void)dealloc {
    [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


    _logoImage.layer.cornerRadius = 30;
    [self loadPhotos];

    [_libraryOutlet addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handle_data) name:@"reload_data" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideMenu) name:@"hide_menu" object:nil];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // Begin caching assets in and around collection view's visible rect.
    [self updateCachedAssets];
}

-(void)handle_data {
    //[self.collectionView2 layoutIfNeeded];
    //[self resetCachedAssets];

    [self.collectionView2 reloadData];
    [self updateCachedAssets];
    NSLog(@"did it work?");
}

- (void)viewDidLayoutSubviews
{
    NSInteger section = [self.collectionView2 numberOfSections] - 1;
    NSInteger item = [self.collectionView2 numberOfItemsInSection:section] - 1;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
    [self.collectionView2 scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionTop) animated:NO];
    //[self loadPhotos];
}

-(void) loadPhotos {

    PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

    PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];


    if (self.assetsFetchResults == nil) {
        self.assetsFetchResults = allPhotos;
    }
}



#pragma mark - PHPhotoLibraryChangeObserver

- (void)photoLibraryDidChange:(PHChange *)changeInstance {
    // Check if there are changes to the assets we are showing.
    PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.assetsFetchResults];
    if (collectionChanges == nil) {
        return;
    }

    /*
     Change notifications may be made on a background queue. Re-dispatch to the
     main queue before acting on the change as we'll be updating the UI.
     */
    dispatch_async(dispatch_get_main_queue(), ^{
        // Get the new fetch result.
        self.assetsFetchResults = [collectionChanges fetchResultAfterChanges];

        UICollectionView *collectionView = self.collectionView;

        if (![collectionChanges hasIncrementalChanges] || [collectionChanges hasMoves]) {
            // Reload the collection view if the incremental diffs are not available
            [collectionView reloadData];

        } else {
            /*
             Tell the collection view to animate insertions and deletions if we
             have incremental diffs.
             */
            [collectionView performBatchUpdates:^{
                NSIndexSet *removedIndexes = [collectionChanges removedIndexes];
                if ([removedIndexes count] > 0) {
                    [collectionView deleteItemsAtIndexPaths:[removedIndexes aapl_indexPathsFromIndexesWithSection:0]];
                }

                NSIndexSet *insertedIndexes = [collectionChanges insertedIndexes];
                if ([insertedIndexes count] > 0) {
                    [collectionView insertItemsAtIndexPaths:[insertedIndexes aapl_indexPathsFromIndexesWithSection:0]];
                }

                NSIndexSet *changedIndexes = [collectionChanges changedIndexes];
                if ([changedIndexes count] > 0) {
                    [collectionView reloadItemsAtIndexPaths:[changedIndexes aapl_indexPathsFromIndexesWithSection:0]];
                }
            } completion:NULL];
        }

        [self resetCachedAssets];
    });
}



#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.assetsFetchResults.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; {

    CGFloat colum = 3.0, spacing = 0.0;
    CGFloat value = floorf((CGRectGetWidth(self.view.bounds) - (colum - 1) * spacing) / colum);

    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize                     = CGSizeMake(value, value);
    layout.sectionInset                 = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing      = spacing;
    layout.minimumLineSpacing           = spacing;


    return CGSizeMake(value, value);
    //return self.collectionView.frame.size;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    PHAsset *asset = self.assetsFetchResults[indexPath.item];

    // Dequeue an AAPLGridViewCell.
    AAPLGridViewCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellReuseIdentifier forIndexPath:indexPath];
    cell.representedAssetIdentifier = asset.localIdentifier;


    // Request an image for the asset from the PHCachingImageManager.
    [self.imageManager requestImageForAsset:asset
                                 targetSize:CGSizeMake(130, 130)
                                contentMode:PHImageContentModeAspectFill
                                    options:nil
                              resultHandler:^(UIImage *result, NSDictionary *info) {
                                  // Set the cell's thumbnail image if it's still showing the same asset.
                                  if ([cell.representedAssetIdentifier isEqualToString:asset.localIdentifier]) {
                                      cell.thumbnailImage = result;
                                  }
                              }];
    CGPoint bottomOffset = CGPointMake(-0, self.collectionView.contentSize.height - self.collectionView.bounds.size.height + self.collectionView.contentInset.bottom);
    [self.collectionView setContentOffset:bottomOffset animated:NO];;
        return cell;

}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Prepare the options to pass when fetching the live photo.
    PHAsset *asset = self.assetsFetchResults[indexPath.item];
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    options.networkAccessAllowed = NO;

    dispatch_async(dispatch_get_main_queue(), ^{
        _indicatorView = [ImageEditorTheme indicatorView];
        _indicatorView.center = self.containerView.center;
        [self.containerView addSubview:_indicatorView];
        [_indicatorView startAnimating];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PreviewVC *prevVC = (PreviewVC *)[storyboard instantiateViewControllerWithIdentifier:@"PreviewVC"];

    [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {


            // Show the UIImageView and use it to display the requested image.
            passedImage = result;

            prevVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self presentViewController:prevVC animated:true completion:nil];
        [_indicatorView stopAnimating];
        }];
    });
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // Update cached assets for the new visible area.
    [self updateCachedAssets];
}
EN

回答 1

Stack Overflow用户

发布于 2017-01-04 17:00:52

我设法解决了这个问题。这就像删除"awakeFromNib“中对"self resetCachedAssets;”的调用一样简单。

现在效果很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41158305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档