web-dev-qa-db-fra.com

UiImage avec des coins arrondis

J'ai déjà lu plusieurs messages. Et presque tout le monde suggère d'utiliser QuartzCore/QuartzCore.h avec layer.cornerRadius

J'ai essayé cette méthode et quelques méthodes supplémentaires.

Eh bien, tout fonctionne bien quand une image ne bouge pas.

Mais dans mon projet, je déplace toujours mes images. Si j'ajoute un rayon d'angle ou une ombre, le mouvement de l'image n'est plus lisse. Et ça a l'air bien!

C'est comme ça que je redimensionniste mon image:

CGSize newSize = CGSizeMake(200*height/image.size.height, 280);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, newSize.width, newSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClearRect(context, CGRectMake(0, 0, newSize.width, newSize.height));
CGContextDrawImage(context, CGRectMake(0, 0, newSize.width, newSize.height), image.CGImage);
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
UIImage *newImage = [UIImage imageWithCGImage: scaledImage];
CGImageRelease(scaledImage);

J'aimerais connaître un bon moyen de redimensionner mon image, d'ajouter une ombre, un rayon d'angle et d'avoir un mouvement en douceur de mon image.

20
iWheelBuy
// Get your image somehow
UIImage *image = [UIImage imageNamed:@"image.jpg"];

// Begin a new image that will be the new image with the rounded corners 
// (here with the size of an UIImageView)
 UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);

 // Add a clip before drawing anything, in the shape of an rounded rect
  [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                        cornerRadius:10.0] addClip];
 // Draw your image
[image drawInRect:imageView.bounds];

 // Get the image, here setting the UIImageView image
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();

 // Lets forget about that we were drawing
  UIGraphicsEndImageContext();

Essayez de vous déplacer avec ce code, autant que je me souvienne de me souvienne, j'ai utilisé cela un certain temps qui fait une image avec des coins arrondis que vous pouvez déplacer dans les objectifs. Mais cela semblait aller à l'échelle amende ...

70
MCKapur

La réponse de Rohan est une excellente. Si quiconque a des problèmes qui le refactent pour travailler dans leurs projets ici est un refacteur soigné susceptible d'aider certains nouveaux programmeurs:

+ (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius usingImage:(UIImage *)original
{    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:original];

    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);

    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                            cornerRadius:cornerRadius] addClip];
    // Draw your image
    [original drawInRect:imageView.bounds];

    // Get the image, here setting the UIImageView image
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();

    return imageView.image;
}
12
Charlie Seligman

Il existe une version optimisée de la solution de solution de Charlie Seligman. Vous n'avez pas besoin d'utiliser UIImageView à cette fin.

+ (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius usingImage:(UIImage *)original
{
    CGRect frame = CGRectMake(0, 0, original.size.width, original.size.height);

    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContextWithOptions(original.size, NO, original.scale);

    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:frame
                                cornerRadius:cornerRadius] addClip];
    // Draw your image
    [original drawInRect:frame];

    // Get the image, here setting the UIImageView image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();

    return image;
}

Vous pouvez obtenir l'image ronde en spécifiant Crainradius égal à une taille d'image à deux reprises (image.size.width * 2).

10
alexmorhun

Si la performance est mauvaise lors de l'animation de la position de l'image, c'est probablement parce qu'il doit avoir à rendre l'image avec le rayon de coin chaque image. Ceci est inévitable si, derrière l'image, il n'ya pas de couleur unie (le "mélange" est différent dans chaque cadre et doit donc être calculé). Si ce n'est pas le cas, et vous pouvez obtenir l'arrière-plan d'une couleur unie, assurez-vous de définir le UIView.backgroundColor à quelque chose d'autre que ClearColor, avec un alpha 1.0. Rasterizing sur la couche peut également vous aider (il stockera la version mise en cache du calque rendu en mémoire et l'utilisera tout au long de l'animation. Mais à nouveau, ne vous aidera pas si la couche n'est pas opaque).

Voici comment vous faites ceci:

UIView *yourView;
CALayer *yourLayer = yourView.layer;

yourLayer.shouldRasterize = YES;
yourLayer.rasterizationScale = [UIScreen mainScreen].scale;
1
Javier Soto

Définir des coins arrondis sur une image dans l'objectif-c:

yourImageView.layer.cornerRadius = yourRadius;                                   

yourImageView.clipsToBounds = YES;                                 
1
Kiran Bhoraniya

Oui c'est possible.

Importer l'en-tête Quartzcore (#Import) et jouer avec la propriété COUCHE de l'UIImageView.

ImageView.layer.cornerRadius = Radius;
ImageView.clipsToBounds = YES;
1
Dixit Patel