2010-09-26 10 views
5

¿Hay una manera de fijar las fronteras a un UIImage. Yo sé cómo configurarlo para UIImageView, pero mi problema es el UIImage que me carga en un UIImageView suele ser del mismo tamaño o la relación de aspecto que la UIImageView. Por lo tanto, he mantenido el modo UIImageView en aspecto de aspecto. Dando un borde a UIImageView ahora sería bordear toda la UIImageView en lugar de sólo la UIImage, y que no se ve bien cuando el UIImage no es del mismo tamaño o la relación de aspecto que la UIV.Configuración de una frontera UIImage

¿Ayuda?

Respuesta

3

Hay algunas soluciones en esta pregunta: How can i take an UIImage and give it a black border?

+0

dos problemas. Esa otra pregunta es acerca de renderizar un UIImage directamente, no usando un UIImageView. Además, la mejor solución coloca el borde alrededor del marco del UIImageView, no la imagen del aspecto ajustado dentro del UIImageView. –

+0

En realidad, la otra pregunta es similar, es solo la respuesta popular que no ayuda aquí. –

0
image with border 

+(UIImage*)imageWithBorderFromImage:(UIImage*)source 
{ 

    CGSize size = [source size]; 

    UIGraphicsBeginImageContext(size); 
    CGRect rect = CGRectMake(0, 0, size.width, size.height); 
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context,(255/255.f),(255/255.f),(255/255.f), 1.0); 
    CGContextStrokeRect(context, rect); 
    UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return testImg; 
} 
+0

¿Cómo puedo establecer el ancho del borde? – alexmorhun

1
#import <QuartzCore/CALayer.h> 


UIImageView *imageView = [UIImageView alloc]init]; 
imageView.layer.masksToBounds = YES; 
imageView.layer.borderColor = [UIColor blackColor].CGColor; 
imageView.layer.borderWidth = 1; 
imageView.layer.cornerRadius = 10; //optional