Skip to content

Commit 9eb56f4

Browse files
authored
[WCiOS17] Address warnings from WPMediaPicker deprecations (#16407)
2 parents ae62e79 + 7e5a18c commit 9eb56f4

File tree

6 files changed

+56
-223
lines changed

6 files changed

+56
-223
lines changed

Modules/Sources/WPMediaPicker/WPImageExporter.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
@import MobileCoreServices;
44
@import ImageIO;
5+
@import UniformTypeIdentifiers;
56

67
@implementation WPImageExporter
78

@@ -33,7 +34,7 @@ + (BOOL)writeImage:(UIImage *)image withMetadata:(NSDictionary *)metadata toURL:
3334
adjustedMetadata[(NSString *)kCGImagePropertyIPTCDictionary] = adjustedIPTC;
3435
}
3536

36-
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)fileURL, kUTTypeJPEG, 1, nil);
37+
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)fileURL, (CFStringRef)UTTypeJPEG.identifier, 1, nil);
3738
if (destination == NULL) {
3839
return NO;
3940
}

Modules/Sources/WPMediaPicker/WPInputMediaPickerViewController.m

Lines changed: 0 additions & 134 deletions
This file was deleted.

Modules/Sources/WPMediaPicker/WPMediaCapturePresenter.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
@import MobileCoreServices;
55
@import AVFoundation;
6+
@import UniformTypeIdentifiers;
67

78
@interface WPMediaCapturePresenter () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
89
@property (nonatomic, strong, nullable) UIViewController *presentingViewController;
@@ -79,11 +80,10 @@ - (void)presentCaptureViewController
7980
UIImagePickerControllerSourceTypeCamera]];
8081
NSMutableSet *mediaDesired = [NSMutableSet new];
8182
if (self.mediaType & WPMediaTypeImage) {
82-
[mediaDesired addObject:(__bridge NSString *)kUTTypeImage];
83+
[mediaDesired addObject:UTTypeImage.identifier];
8384
}
8485
if (self.mediaType & WPMediaTypeVideo) {
85-
[mediaDesired addObject:(__bridge NSString *)kUTTypeMovie];
86-
86+
[mediaDesired addObject:UTTypeMovie.identifier];
8787
}
8888
if (mediaDesired.count > 0){
8989
[mediaTypes intersectSet:mediaDesired];

Modules/Sources/WPMediaPicker/WPMediaCapturePreviewCollectionView.m

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ - (void)startCapture
110110
CALayer *viewLayer = self.previewView.layer;
111111
self.captureVideoPreviewLayer.frame = viewLayer.bounds;
112112
self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
113-
UIWindowScene *currentScene = [[[[UIApplication sharedApplication] windows] lastObject] windowScene];
114-
self.captureVideoPreviewLayer.connection.videoOrientation = [self videoOrientationForInterfaceOrientation:[currentScene interfaceOrientation]];
113+
UIWindowScene *currentScene = [self activeWindowScene];
114+
self.captureVideoPreviewLayer.connection.videoRotationAngle = [self videoRotationAngleForInterfaceOrientation:[currentScene interfaceOrientation]];
115115
[viewLayer addSublayer:self.captureVideoPreviewLayer];
116116
});
117117
}
@@ -121,24 +121,39 @@ - (void)startCapture
121121

122122
- (void)deviceOrientationDidChange:(NSNotification *)notification
123123
{
124-
if (self.captureVideoPreviewLayer.connection.supportsVideoOrientation) {
125-
UIWindowScene *currentScene = [[[[UIApplication sharedApplication] windows] lastObject] windowScene];
126-
self.captureVideoPreviewLayer.connection.videoOrientation = [self videoOrientationForInterfaceOrientation:[currentScene interfaceOrientation]];
124+
UIWindowScene *currentScene = [self activeWindowScene];
125+
self.captureVideoPreviewLayer.connection.videoRotationAngle = [self videoRotationAngleForInterfaceOrientation:[currentScene interfaceOrientation]];
126+
}
127+
128+
- (UIWindowScene *)activeWindowScene
129+
{
130+
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
131+
if ([scene isKindOfClass:[UIWindowScene class]] && scene.activationState == UISceneActivationStateForegroundActive) {
132+
return (UIWindowScene *)scene;
133+
}
134+
}
135+
// Fallback to first window scene if no active one found
136+
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
137+
if ([scene isKindOfClass:[UIWindowScene class]]) {
138+
return (UIWindowScene *)scene;
139+
}
127140
}
141+
return nil;
128142
}
129143

130-
- (AVCaptureVideoOrientation)videoOrientationForInterfaceOrientation:(UIInterfaceOrientation)orientation
144+
- (CGFloat)videoRotationAngleForInterfaceOrientation:(UIInterfaceOrientation)orientation
131145
{
132146
switch (orientation) {
133147
case UIInterfaceOrientationPortrait:
134-
return AVCaptureVideoOrientationPortrait;
148+
return 90.0;
135149
case UIInterfaceOrientationPortraitUpsideDown:
136-
return AVCaptureVideoOrientationPortraitUpsideDown;
150+
return 270.0;
137151
case UIInterfaceOrientationLandscapeLeft:
138-
return AVCaptureVideoOrientationLandscapeLeft;
152+
return 180.0;
139153
case UIInterfaceOrientationLandscapeRight:
140-
return AVCaptureVideoOrientationLandscapeRight;
141-
default:return AVCaptureVideoOrientationPortrait;
154+
return 0.0;
155+
default:
156+
return 90.0;
142157
}
143158
}
144159

Modules/Sources/WPMediaPicker/WPMediaPickerViewController.m

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#import "WPMediaGroupPickerViewController.h"
66
#import "WPPHAssetDataSource.h"
77
#import "WPMediaCapturePresenter.h"
8-
#import "WPInputMediaPickerViewController.h"
98
#import "WPCarouselAssetsViewController.h"
109
#import "UIViewController+MediaAdditions.h"
1110

1211
@import MobileCoreServices;
1312
@import AVFoundation;
13+
@import UniformTypeIdentifiers;
1414

1515
static CGFloat const IPhoneSELandscapeWidth = 568.0f;
1616
static CGFloat const IPhone7PortraitWidth = 375.0f;
@@ -114,7 +114,8 @@ - (void)viewDidLoad
114114
[self.view addGestureRecognizer:self.longPressGestureRecognizer];
115115

116116
self.layout.sectionInsetReference = UICollectionViewFlowLayoutSectionInsetFromSafeArea;
117-
117+
[self registerForTraitObservation];
118+
118119
[self refreshDataAnimated:NO];
119120
}
120121

@@ -317,18 +318,6 @@ - (void)viewDidDisappear:(BOOL)animated
317318
[self unregisterForKeyboardNotifications];
318319
}
319320

320-
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
321-
{
322-
[super traitCollectionDidChange:previousTraitCollection];
323-
324-
if ([self shouldShowCustomHeaderView]) {
325-
// If there's a custom header, we'll invalidate it so that it can adapt itself to dynamic type changes.
326-
UICollectionViewFlowLayoutInvalidationContext *context = [UICollectionViewFlowLayoutInvalidationContext new];
327-
[context invalidateSupplementaryElementsOfKind:UICollectionElementKindSectionHeader atIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0] ]];
328-
[self.collectionView.collectionViewLayout invalidateLayout];
329-
}
330-
}
331-
332321
- (UIViewController *)viewControllerToUseToPresent
333322
{
334323
// viewControllerToUseToPresent defaults to self but could be set to nil. Reset to self if needed.
@@ -381,7 +370,6 @@ - (void)addCollectionViewToView
381370
- (void)setupSearchBar
382371
{
383372
BOOL shouldShowSearchBar = self.options.showSearchBar &&
384-
![self.parentViewController isKindOfClass:[WPInputMediaPickerViewController class]] && //Disable search bar on WPInputMediaPicker
385373
[self.dataSource respondsToSelector:@selector(searchFor:)];
386374

387375
if (shouldShowSearchBar && self.searchBar == nil) {
@@ -999,7 +987,8 @@ - (void)configureBadgeViewForCell:(WPMediaCollectionViewCell *)cell withAsset:(i
999987
NSString *uttype = [asset UTTypeIdentifier];
1000988

1001989
if ([self.options.badgedUTTypes containsObject:uttype]) {
1002-
NSString *tagName = (__bridge_transfer NSString *)(UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uttype, kUTTagClassFilenameExtension));
990+
UTType *type = [UTType typeWithIdentifier:uttype];
991+
NSString *tagName = type.preferredFilenameExtension;
1003992
cell.badgeView.label.text = [tagName uppercaseString];
1004993
cell.badgeView.hidden = NO;
1005994
return;
@@ -1274,12 +1263,12 @@ - (void)processMediaCaptured:(NSDictionary *)info
12741263
}
12751264
[self addMedia:media animated:YES];
12761265
};
1277-
if ([info[UIImagePickerControllerMediaType] isEqual:(NSString *)kUTTypeImage]) {
1266+
if ([info[UIImagePickerControllerMediaType] isEqual:UTTypeImage.identifier]) {
12781267
UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage];
12791268
[self.dataSource addImage:image
12801269
metadata:info[UIImagePickerControllerMediaMetadata]
12811270
completionBlock:completionBlock];
1282-
} else if ([info[UIImagePickerControllerMediaType] isEqual:(NSString *)kUTTypeMovie]) {
1271+
} else if ([info[UIImagePickerControllerMediaType] isEqual:UTTypeMovie.identifier]) {
12831272
[self.dataSource addVideoFromURL:info[UIImagePickerControllerMediaURL] completionBlock:completionBlock];
12841273
}
12851274
}
@@ -1466,20 +1455,30 @@ - (BOOL)isPresentedAsPopover
14661455
return NO;
14671456
}
14681457

1458+
- (void)registerForTraitObservation
1459+
{
1460+
__weak typeof(self) weakSelf = self;
1461+
[self registerForTraitChanges:@[UITraitPreferredContentSizeCategory.class]
1462+
withHandler:^(id<UITraitEnvironment> traitEnvironment, UITraitCollection *previousCollection) {
1463+
if ([weakSelf shouldShowCustomHeaderView]) {
1464+
// If there's a custom header, invalidate it so that it can adapt itself to dynamic type changes.
1465+
UICollectionViewFlowLayoutInvalidationContext *context = [UICollectionViewFlowLayoutInvalidationContext new];
1466+
[context invalidateSupplementaryElementsOfKind:UICollectionElementKindSectionHeader atIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0] ]];
1467+
[weakSelf.collectionView.collectionViewLayout invalidateLayout];
1468+
}
1469+
}];
1470+
}
1471+
14691472
- (void)registerForKeyboardNotifications
14701473
{
1471-
if (![self.parentViewController isKindOfClass:[WPInputMediaPickerViewController class]]) {
1472-
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
1473-
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
1474-
}
1474+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
1475+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
14751476
}
14761477

14771478
- (void)unregisterForKeyboardNotifications
14781479
{
1479-
if (![self.parentViewController isKindOfClass:[WPInputMediaPickerViewController class]]) {
1480-
[NSNotificationCenter.defaultCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
1481-
[NSNotificationCenter.defaultCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
1482-
}
1480+
[NSNotificationCenter.defaultCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
1481+
[NSNotificationCenter.defaultCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
14831482
}
14841483

14851484
- (void)keyboardWillShowNotification:(NSNotification *)notification

Modules/Sources/WPMediaPicker/include/WPInputMediaPickerViewController.h

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)