Массив, Ищущий проблему кода

Быстрое решение для Stretch Fill, Aspect Fill и Aspect Fit

extension UIImage {
    enum ContentMode {
        case contentFill
        case contentAspectFill
        case contentAspectFit
    }

    func resize(withSize size: CGSize, contentMode: ContentMode = .contentAspectFill) -> UIImage? {
        let aspectWidth = size.width / self.size.width;
        let aspectHeight = size.height / self.size.height;

        switch contentMode {
        case .contentFill:
            return resize(withSize: size)
        case .contentAspectFit:
            let aspectRatio = min(aspectWidth, aspectHeight)
            return resize(withSize: CGSize(width: self.size.width * aspectRatio, height: self.size.height * aspectRatio))
        case .contentAspectFill:
            let aspectRatio = max(aspectWidth, aspectHeight)
            return resize(withSize: CGSize(width: self.size.width * aspectRatio, height: self.size.height * aspectRatio))
        }
    }

    private func resize(withSize size: CGSize) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(size, false, 1)
        defer { UIGraphicsEndImageContext() }
        draw(in: CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

и для использования вы можете сделать следующее:

let image = UIImage(named: "image.png")!
let newImage = image.resize(withSize: CGSize(width: 200, height: 150), contentMode: .contentAspectFill)

Благодаря abdullahselek за его оригинальное решение.

10
задан 5 revs, 2 users 100% 8 July 2009 в 04:43
поделиться

33 ответа

ВОЗДУШНЫЙ КОД PHP 285 СИМВОЛОВ

function f($a,$b){
  if ( count($a) < 1 ) return -1;
  if ( count($b) < 1 ) return -1;
  if ( count($a) < count($b) ) return -1;

  $x = array_shift($a);
  $z = array_shift($b);

  if ($x != $z){
    $r = f( $x, array_unshift($z, $b) );
    return (-1 == $r) ? -1 : 1 + $r;
  }

  $r = f($a, $b);
  return (-1 == $r) ? -1 : 0;

}

0
ответ дан 3 December 2019 в 13:13
поделиться

C #

Для списков:

public static int IndexOf<T>( List<T> list1, List<T> list2 )
{
    return !list2.Except( list1 ).Any() ? list1.IndexOf( list2[0] ) : -1;
}

Для массивов:

public static int IndexOf<T>( T[] arr1, T[] arr2 )
{
    return !arr2.Except( arr1 ).Any() ? Array.IndexOf( arr1, arr2[0] ) : -1;
}
-2
ответ дан 3 December 2019 в 13:13
поделиться

Golfscript - 35 байтов

Только 32 байта для фактической функции, если мы посчитаем то же самое. как для J

{:b;:a,,{.a@>b,<b={}{;}if}%-1or}:f;

#Test cases (same as J)               output
[63 110 245 215 0] [245 215] f p   #  [2]
[22 55 74 3 1] [24 56 74] f p      #  -1
[1 1 1 2 1 1 3] [1 1]f p           #  [0 1 4]
[0 1 2 3 1 0 2 1 2 0] [1 2] f p    #  [1 7]
0
ответ дан 3 December 2019 в 13:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: