Как Повернуть 90 градусов UIImage?

Честно, я думаю, что Ваше лучшее решение состоит в том, чтобы заставить объекты во время Woot-прочь только быть видимыми к, вошел в систему пользователи, и ограничьте каждого зарегистрированного пользователя одним обновлением домашней страницы каждые 500 мс или около этого. (Или возможно заставьте только изображение объекта быть видимым неаутентифицируемым пользователям во время Woot-прочь и удостовериться, что Вы не всегда используете то же изображение для Случайного Дерьма.) Я думаю, что пользователи Woot были бы готовы принять это, если Вы продаете его в качестве меры, чтобы помочь им получить свои Миски Сливочности, и можно также указать, что это поможет им проверить более быстрый. Что-либо еще - даже использование капчей - подвергается Вашей типичной гонке вооружений.

174
задан Kara 8 June 2016 в 16:00
поделиться

2 ответа

Что насчет чего-то вроде:

static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, radians(90));
    } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, radians(-90));
    } else if (orientation == UIImageOrientationDown) {
        // NOTHING
    } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, radians(90));
    }

    [src drawAtPoint:CGPointMake(0, 0)];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
87
ответ дан 23 November 2019 в 20:27
поделиться

resize-a-uiimage-the-right-way объясняет некоторые проблемы, которые есть во многих примерах кода для этого, и содержит некоторые фрагменты кода, помогающие справиться с UIImages - частным вспомогательным методом в UIImage + resize. m принимает преобразование, разрешающее вращение, поэтому вам просто нужно предоставить его как общедоступный интерфейс.

// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
// If the new size is not integral, it will be rounded up
- (UIImage *)resizedImage:(CGSize)newSize
                transform:(CGAffineTransform)transform
           drawTransposed:(BOOL)transpose
     interpolationQuality:(CGInterpolationQuality)quality {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
    CGImageRef imageRef = self.CGImage;

    // Build a context that's the same dimensions as the new size
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                CGImageGetBitmapInfo(imageRef));

    // Rotate and/or flip the image if required by its orientation
    CGContextConcatCTM(bitmap, transform);

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(bitmap, quality);

    // Draw into the context; this scales the image
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;
}

Это лицензия из этого файла:

// Created by Trevor Harmon on 8/5/09.
// Free for personal or commercial use, with or without modification.
// No warranty is expressed or implied.
0
ответ дан 23 November 2019 в 20:27
поделиться