1

我的应用程序中有一个部分,用户可以在其中快速拍摄照片并将其保存到相机胶卷中。我唯一的问题是,在拍完照片后,它会用方形裁剪器出现在屏幕上(我猜就是这样),然后在底部显示“重拍”和“使用照片”。我希望使用照片说“保存照片”(无论如何它就是这样做的)。有没有办法做到这一点?这是我的代码

- (IBAction)takePhoto:(UIButton *)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];
}
4

3 回答 3

3

您可以将取消按钮更改为您想要的任何标题:

  1. UINavigationControllerDelegate委托添加到您将单击按钮并显示相机的 viewController
  2. 在您的 viewController 中添加以下代码:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self changeCancelToAnythingUWantOf:viewController andReplacementString:@"Your Replacement String"];
    

    }

    - (void)changeCancelToAnythingUWantOf:(UIViewController*)vc andReplacementString:(NSString*)title {
    
    for(UIView *subView in vc.view.subviews) {
     if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *btn = (UIButton *)subView;
        if([btn.titleLabel.text isEqualToString:@"Cancel"]) {
            [btn setTitle:title forState:UIControlStateNormal];
            break;
        }
    }
    
     if (subView.subviews) {
        for (UIView *subSubView in subView.subviews) {
            if ([subSubView isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)subSubView;
                if([btn.titleLabel.text isEqualToString:@"Cancel"]) {
                    [btn setTitle:title forState:UIControlStateNormal];
                    break;
                } 
            }
    
            if (subSubView.subviews) {
                for (UIView *subSubSubView in subSubView.subviews) {
                    if ([subSubSubView isKindOfClass:[UIButton class]]) {
                        UIButton *btn = (UIButton *)subSubSubView;
                        if([btn.titleLabel.text isEqualToString:@"Cancel"]) {
                            [btn setTitle:title forState:UIControlStateNormal];
                            break;
                        } 
                    }
                }
             }
           }
         }
       }
     }
    
于 2014-06-13T17:40:15.790 回答
0

第一个 swizzling 捆绑包

private func swizzle(_ bundle: Bundle.Type) {

    [(#selector(bundle.localizedString(forKey:value:table:)),
      #selector(bundle.rd_localizedString(forKey:value:table:)))]
        .forEach { original, swizzled in

            guard let originalMethod = class_getInstanceMethod(bundle, original),
                let swizzledMethod = class_getInstanceMethod(bundle, swizzled) else { return }

            let didAddMethod = class_addMethod(
                bundle,
                original,
                method_getImplementation(swizzledMethod),
                method_getTypeEncoding(swizzledMethod)
            )

            if didAddMethod {
                class_replaceMethod(
                    bundle,
                    swizzled,
                    method_getImplementation(originalMethod),
                    method_getTypeEncoding(originalMethod)
                )
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod)
            }
    }
}

private var hasSwizzled = false

extension Bundle {
    final public class func
        doBadSwizzleStuff() {
        guard !hasSwizzled else { return }

        hasSwizzled = true
        swizzle(self)

    }

    @objc internal func rd_localizedString(forKey: String, value: String?, table: String?) -> String {
        let bundle = Bundle.main
        return bundle.rd_localizedString(forKey: forKey, value: value, table: table)
    }
}

其次为 UIImagePickerController 创建本地化字符串

下面是我见过的带有键的文件的内容(带有标准英文值),它们非常自我解释

CameraUI.strings

"PHOTO" = "PHOTO";
"AEAF_LOCK_TEXT" = "AE/AF LOCK";
"API_CANCEL_TITLE" = "Cancel";
"HDR_AUTO" = "Auto";
"HDR_ON" = "On";
"HDR_OFF" = "Off";
"TIMER_OFF_TEXT" = "Off";
"USE_PHOTO" = "Use Photo";

PhotoLibraryServices.strings

"PHOTOS" = "Photos";
"CAMERA_ROLL" = "Camera roll";
"ALL_SCREENSHOTS" = "Screenshots";

PhotoLibrary.strings

"CANCEL" = "Cancel";
"RETAKE" = "Retake";
"STREAM_SHARED_BY_ME_SUBTITLE" = "From You";
"STREAM_SHARED_BY_SUBTITLE" = "From %@";
"ALBUM_IMAGE_COUNT_FORMAT" = "%@ Photos";
"ALBUM_VIDEO_COUNT_FORMAT" = "%@ Videos";
"1_ALBUM_PHOTO" = "1 Photo";
"1_ALBUM_VIDEO" = "1 Video";
"ALBUM_TWO_TYPES_LABEL_COMMAS" = "%@, %@";

照片UI.strings

"ALL_PHOTOS_IN_LIBRARY" = "Moments";
"PXUserCollectionsSectionTitle" = "My Albums";
"FULL_PHOTOS_GRID_ZOOM_LEVEL_TITLE" = "Moments";
"NO_PHOTOS_OR_VIDEOS" = "No Photos or Videos";

第三次实现你的本地化类

internal enum L10n {
  internal enum CameraUI {
    /// AE/AF LOCK
    internal static let aeafLockText = L10n.tr("CameraUI", "AEAF_LOCK_TEXT")
    /// Cancel
    internal static let apiCancelTitle = L10n.tr("CameraUI", "API_CANCEL_TITLE")
    /// Auto
    internal static let hdrAuto = L10n.tr("CameraUI", "HDR_AUTO")
    /// Off
    internal static let hdrOff = L10n.tr("CameraUI", "HDR_OFF")
    /// On
    internal static let hdrOn = L10n.tr("CameraUI", "HDR_ON")
    /// PHOTO
    internal static let photo = L10n.tr("CameraUI", "PHOTO")
    /// Off
    internal static let timerOffText = L10n.tr("CameraUI", "TIMER_OFF_TEXT")
    /// Use test
    internal static let usePhoto = L10n.tr("CameraUI", "USE_PHOTO")
  }

    // MARK: - Implementation Details

extension L10n {
  private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
    // swiftlint:disable:next nslocalizedstring_key
    let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "")
    return String(format: format, locale: Locale.current, arguments: args)
  }
}

private final class BundleToken {}
于 2019-05-07T21:35:24.173 回答
0

你可以用recursion方式找到这个

func buttonWithTitle(forView view: UIView, title: String) -> UIButton? {
    guard view.subviews.count > 0 else { return nil }
    var needButton: UIButton? = nil
    for subview in view.subviews {
        if subview is UIButton {
            let button = subview as! UIButton
            if button.titleLabel?.text == title {
                return button
            }
        }

        needButton = buttonWithTitle(forView: subview, title: title)
        if needButton != nil { return needButton }
    }

    return needButton
}
于 2016-05-26T15:01:36.440 回答