小编Giz*_*odo的帖子

PHAssets/PHAssetCollection/PHCollectionList 和文件夹

我正在遵循苹果的示例代码“ ExampleappusingPhotosframework ”。

在 PHAssets/PHAssetCollection 主题中,我注意到可能存在“文件夹”。这些可以通过 Mac 的照片应用程序或较旧的 iPhoto 应用程序等创建而出现。

文件夹可能存在 PHAssetCollection 数量,并且该文件夹可能包含未知级别的嵌套文件夹,最后以相册结尾。

因此,如果我在照片库中有嵌套文件夹,即使上面的示例项目也会崩溃。大多数应用程序似乎只是忽略并且根本不显示这些文件夹。

举个例子,假设这个例子存在:

在此输入图像描述

文件夹级别 1 > 文件夹级别 2 > 自定义相册 > 图像 1、图像 2

iOS 照片应用程序如何处理它:

在此输入图像描述

任务

在根相册列表中的用户相册下,应该可以看到“文件夹级别 1”。点击该按钮后,我想列出所有子文件夹中的所有图像,无论子级别如何......

视图加载

override func viewDidLoad() {
    super.viewDidLoad()

    let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.addAlbum))
    self.navigationItem.rightBarButtonItem = addButton


    // Create a PHFetchResult object for each section in the table view.
    let allPhotosOptions = PHFetchOptions()
    allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
    allPhotos = PHAsset.fetchAssets(with: allPhotosOptions)
    smartAlbums = …
Run Code Online (Sandbox Code Playgroud)

photos ios swift phasset phassetcollection

5
推荐指数
1
解决办法
3908
查看次数

UIVIew翻转垂直动画

鉴于:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:card cache:NO];
 myPic = [UIImage UIImagenamed: @"mySecondImage.png"];
[UIView commitAnimations];[/CODE]
Run Code Online (Sandbox Code Playgroud)

通过翻转动画"myPic"从右向左动画.

我需要获得相同的动画,但是垂直.从顶部翻转或从底部翻转.我环顾四周,没有人真的有建议的工作模型.

我试过这个,但没有运气:

float duration = .5;
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
animation.fromValue = [NSNumber numberWithDouble:0.0f * M_PI];
animation.toValue = [NSNumber numberWithDouble:1.0f * M_PI];
animation.duration = duration;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeBoth;
animation.repeatCount =1;;
animation.beginTime = CACurrentMediaTime();
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
card.layer.anchorPoint = CGPointMake(0.5, 1.0);
[card.layer addAnimation:animation forKey:@"rotationX"];[/CODE]
Run Code Online (Sandbox Code Playgroud)

有什么输入?提前致谢.

iphone animation flip

4
推荐指数
1
解决办法
4028
查看次数

CGImageSource到CGImage?

我得到了以下访问PHAsset的代码.我需要传递一个CGImage:

let imageManager = PHImageManager.defaultManager()
imageManager.requestImageDataForAsset(self.asset!, options: nil, resultHandler:{
    (data, responseString, imageOriet, info) -> Void in
    let imageData: NSData = data!
    if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
        let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary

        //How do I create a CGImage now?

    }
})
Run Code Online (Sandbox Code Playgroud)

我需要提取一个CGImage.遇到麻烦......

xcode cgimage cgimagesource cgimageref swift

4
推荐指数
1
解决办法
1196
查看次数

Swift Observer(KVO):检查是否存在

我有一个用户能够平移的盒子.为此,我添加一个观察者来检查它的中心是否已经改变:

self.boxView!.addObserver(self, forKeyPath: "center", options: .old, context: &BoxCenterContext)
Run Code Online (Sandbox Code Playgroud)

在呈现框的动画之后添加此内容.

当盒子解散时,我将其删除为:

self.boxView!.removeObserver(self, forKeyPath: "center", context: &BoxCenterContext)
Run Code Online (Sandbox Code Playgroud)

问题

用户能够在框演示完成之前解除该框,即.在添加KVO之前.

当发生这种情况时,应用程序崩溃尝试删除不存在的KVO.

有没有办法检查KVO的存在(在尝试删除之前)?

xcode key-value-observing ios swift

4
推荐指数
2
解决办法
1641
查看次数

iOS 等宽自定义字体

我的 Xcode 7、iOS 9 目标项目中包含自定义字体。我想让字体变成等宽字体。我尝试了这个,但没有成功:

let originalFont = UIFont(name: "My Custom Font", size: 18)
let originalFontDescriptor = originalFont!.fontDescriptor()

let fontDescriptorFeatureSettings = [
    [
        UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
        UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector
    ]
]

let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
let fontDescriptor = originalFontDescriptor.fontDescriptorByAddingAttributes(fontDescriptorAttributes)
let font = UIFont(descriptor: fontDescriptor, size: 0)

topLabel.font = font
Run Code Online (Sandbox Code Playgroud)

无论有或没有上述代码,标签都会以正确的自定义字体显示。只是上面的代码没有做任何事情。

fonts monospace core-text ios

3
推荐指数
1
解决办法
3560
查看次数

iOS 13 导航栏大标题问题

我试图Title在 a 中显示一个大的Navigation bar,但背景清晰。向上滚动时,它将Navigation bar具有模糊效果。

在此处输入图片说明

在此处输入图片说明

这看起来是正确的,但是,在滚动时,动画似乎被破坏了。此外,过渡有时会卡住:

在此处输入图片说明

在此处输入图片说明

我的代码如下:

UINavigationController

override func viewDidLoad() {
   super.viewDidLoad()

   if #available(iOS 13.0, *) {

      self.navigationBar.prefersLargeTitles = true

      let style = UINavigationBarAppearance()
      style.configureWithDefaultBackground()

      style.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18)]

      self.navigationBar.standardAppearance = style
      self.navigationBar.compactAppearance = style


      //Configure Large Style
      let largeStyle = UINavigationBarAppearance()
      largeStyle.configureWithTransparentBackground()

      largeStyle.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 28)]

      self.navigationBar.scrollEdgeAppearance = largeStyle

   }
}
Run Code Online (Sandbox Code Playgroud)

UITableView是内部的UINavigationController。两者都是通过 segue 方式从故事板中提取的。

xcode uinavigationbar ios swift uinavigationbarappearance

3
推荐指数
1
解决办法
3779
查看次数

当 CTFeatureTypeIdentifier 不可用时如何使用 UIFontDescriptor

我使用以下代码片段来获取字体功能:

let font = UIFont.systemFont(ofSize: 16)
let features: NSArray = CTFontCopyFeatures(font)!
print("properties = \(features)")
Run Code Online (Sandbox Code Playgroud)

所以如果我看到这个:

{
CTFeatureTypeExclusive = 1;
CTFeatureTypeIdentifier = 22;
CTFeatureTypeName = "Text Spacing";
CTFeatureTypeSelectors =         (
                {
        CTFeatureSelectorDefault = 1;
        CTFeatureSelectorIdentifier = 7;
        CTFeatureSelectorName = "No Change";
    },
                {
        CTFeatureSelectorIdentifier = 8;
        CTFeatureSelectorName = "No Kerning";
    }
);
Run Code Online (Sandbox Code Playgroud)

我可以将其翻译成 Swift:

 let fontDescriptorFeatureSettings = [
            [ UIFontDescriptor.FeatureKey.featureIdentifier : 22,
              UIFontDescriptor.FeatureKey.typeIdentifier : 8], ]
Run Code Online (Sandbox Code Playgroud)

这成功地为我禁用了字距调整。

在同一个片段中,我看到:

{
CTFeatureOpenTypeTag = cv05;
CTFeatureSampleText = I;
CTFeatureTooltipText = "\U00a9 2015-2022 Apple Inc. …
Run Code Online (Sandbox Code Playgroud)

xcode ios ctfont uifontdescriptor

3
推荐指数
1
解决办法
338
查看次数

iOS AVFoundation和多线程

在Apple的AVFoundation演示的每个示例代码中,所有会话任务都有一个单独的队列.

dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL);
Run Code Online (Sandbox Code Playgroud)

这也用于观察焦点变化,曝光变化,白平衡变化等:

- (void)viewWillAppear:(BOOL)animated
{
     [super viewWillAppear:animated];
     dispatch_async([self sessionQueue], ^{
          [self addObservers];
          [[self session] startRunning];
     });
}

- (void)viewDidDisappear:(BOOL)animated
{
     dispatch_async([self sessionQueue], ^{
          [[self session] stopRunning];
          [self removeObservers];
     });
     [super viewDidDisappear:animated];

}
Run Code Online (Sandbox Code Playgroud)

当我遵循这个时,一切都很顺利,直到我采用'GPUImage',第三方api.我被告知这个api不需要多线程或单独排队,因为它全部都在内部处理.所以我完全取消了'sessionQueue'.

但是,现在我的应用程序运行起来非常不稳定,我认为这是由于主队列被告知处理大多数分支任务.

我可以有一个单独的队列来观察self.videoCamera的变化吗?这会占用mainQueue的大部分负担吗?

multithreading avfoundation grand-central-dispatch ios gpuimage

2
推荐指数
1
解决办法
1670
查看次数

AVCapture?照片?输出为FlashScene键值观察

我正在关注 Apple 的最新示例代码AVCam Swift,该代码已更新为使用AVCapture\xe2\x80\x8bPhoto\xe2\x80\x8bOutput

\n\n
\n

var isFlashScene: Bool { 获取 }

\n\n

一个布尔值,指示相机当前预览的场景是否允许使用闪光灯。此属性\xe2\x80\x99s 值会根据相机当前可见的场景而变化。例如,您可以使用此属性突出显示应用程序 xe2x80x99s 相机 UI 中的闪光灯控制,向用户表明场景足够暗,可能需要启用闪光灯。如果照片捕获输出\xe2\x80\x99ssupportedFlashModes 值关闭,则此属性\xe2\x80\x99s 值始终为 false。该属性支持键值观察。

\n
\n\n

我正在尝试键值观察这一点,以便当自动闪光模式指示这是闪光灯将闪光的场景时(就像库存的 iOS 相机应用程序一样),所以我可以更改 UI,就像文档注释一样。

\n\n

所以我这样设置:

\n\n
private let photoOutput = AVCapturePhotoOutput()\n\nprivate var FlashSceneContext = 0\n\nself.addObserver(self, forKeyPath: "photoOutput.isFlashScene", options: .new, context: &FlashSceneContext)\n\noverride func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {\n  if context == & FlashSceneContext {\n     print ("Flash Scene Changed")\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

上面从未显示出任何变化。即使我登录查看

\n\n
print (self.photoOutput.isFlashScene)\n …
Run Code Online (Sandbox Code Playgroud)

key-value-coding avfoundation ios swift avcapturephotosettings

2
推荐指数
1
解决办法
513
查看次数

iOS 13/14 模态视图控制器解除检测

我有一个以模态方式呈现的观点。这是 iOS 13/14 中向下滑动关闭功能的一部分。

当用户开始关闭模态视图时,我收到此委托调用:

func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {

}
Run Code Online (Sandbox Code Playgroud)

一旦用户开始向下拖动视图,就会调用此函数。但是,它可以被取消。

一旦视图完成关闭,以下调用就会给我:

func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {


}
Run Code Online (Sandbox Code Playgroud)

我需要知道用户何时完成关闭手势以及视图将完成关闭

我能够向前迈出几步,但仍然缺少上述所需的检测:

func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {
        
        //User started dismissing, with no guarantee

        self.transitionCoordinator!.animate(alongsideTransition: {(context: UIViewControllerTransitionCoordinatorContext) -> Void in
            
            //Dismissal is animating. Could be finishing or canceling the dismissal

            
        }, completion: {(  context: UIViewControllerTransitionCoordinatorContext) -> Void in
            if context.isCancelled {
                //Dismissal got cancelled
            } else {
                //Dismissal has completed. Too Late!
            }
        })

        
        
    }
Run Code Online (Sandbox Code Playgroud)

xcode dismiss modalviewcontroller ios swift

2
推荐指数
1
解决办法
957
查看次数

UIButton 子类忽略“Touch Up Inside”事件

我有一个 UIButton 的子类:

    import UIKit
    class CustomButton: UIButton {

        required init?(coder decoder: NSCoder) {
            super.init(coder: decoder)

            //Customizing the button appearance
        }
    }
Run Code Online (Sandbox Code Playgroud)

在界面构建器中,在情节提要中,在我的主视图控制器下,我拖放了一个股票按钮,并将其从下拉菜单中的“自定义类”更改为自定义按钮。

将它的“Touch Up Inside”事件连接到 IBAction。

出现了外观的自定义代码,但按钮没有触发 'Touch Up Inside'

当我检查子类中的触摸事件(触摸开始、结束、吃)时,按钮似乎正在接收触摸。

xcode subclass touch uibutton ios

1
推荐指数
1
解决办法
1116
查看次数

iOS:PHAsset和检测DNG RAW格式

我得到了Apple的“ SamplePhotosApp ” 示例代码,并且在相册网格照片布局中,试图检测DNG RAW文件(如果是DNG,则输入徽章)。

默认的cellForItemAt

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let asset = fetchResult.object(at: indexPath.item)

        // Dequeue a GridViewCell.
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: GridViewCell.self), for: indexPath) as? GridViewCell
            else { fatalError("unexpected cell in collection view") }

        // Add a badge to the cell if the PHAsset represents a Live Photo.
        if asset.mediaSubtypes.contains(.photoLive) {
            cell.livePhotoBadgeImage = PHLivePhotoView.livePhotoBadgeImage(options: .overContent)
        }

        // Request an image for the asset from the PHCachingImageManager.
        cell.representedAssetIdentifier …
Run Code Online (Sandbox Code Playgroud)

xcode ios dng uicollectionview phasset

0
推荐指数
1
解决办法
747
查看次数

斯威夫特的懒惰变种

在阅读了Swift的懒惰变量之后,我有以下问题:

class MainViewController: UIViewController {

      lazy var heavyClass = HeavyClass()

      func buttonPressed () {
         //Use heavyClass here
         self.heavyClass.doStuff()
      }    
}
Run Code Online (Sandbox Code Playgroud)

因此,您使用上面的lazy var来优化代码,以便不会立即分配heavyClass.因此,在启动时,这将是最佳的,因为在启动时不会分配heavyClass.

但是,这不会与上面相同吗?

class MainViewController: UIViewController {

      var heavyClass : HeavyClass?

      func buttonPressed () {
         //Use heavyClass here
         self.heavyClass =  HeavyClass()
         self.heavyClass!.doStuff()
      }    
}
Run Code Online (Sandbox Code Playgroud)

var lazy-loading let ios swift

0
推荐指数
1
解决办法
309
查看次数