A UIButton can take the image of a UIImage Objective-C

1

This is my code.

NSArray *sub = [self.view subviews];
if ([sub count] == 0) return;
for (int i=0; i<sub.count; i++){
UIButton *boton = (UIButton *)sub[i];
        //Convierto el boton a UIImage
        UIGraphicsBeginImageContextWithOptions(boton.bounds.size, NO, 0.0);
        [boton.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * imageOriginal = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        // crear un nuevo tamaño, es decir el tamaño de imagen redimensionado
        CGSize sz = CGSizeMake(boton.frame.size.width, boton.frame.size.height);
        // hacer el recorte final rec basado en los valores calculados
        CGRect clipRect = CGRectMake(boton.frame.origin.x, boton.frame.origin.y, boton.frame.size.width, boton.frame.size.height);
        // inicia un nuevo contexto, con el factor de escala 0.0 para que las pantallas retina obtengan una imagen de alta calidad
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            UIGraphicsBeginImageContextWithOptions(sz, NO, 0.0);
        } else {
            UIGraphicsBeginImageContext(sz);
        }
        //Modifica el trazado de recorte actual interceptándolo con el rectángulo especificado
        UIRectClip(clipRect);
        //Dibuja toda la imagen en el rectángulo especificado, escalándola según sea necesario para que se ajuste. Este método dibuja toda la imagen en el contexto gráfico actual, respetando el ajuste de orientación de la imagen. En el sistema de coordenadas por defecto, las imágenes se sitúan abajo ya la derecha del origen del rectángulo especificado. Sin embargo, este método respeta cualquier transformación aplicada al contexto gráfico actual.
        [imageOriginal drawInRect:clipRect];
        //Elimina el contexto de gráficos basado en mapas de bits actual de la parte superior de la pila
        UIGraphicsEndImageContext();
        [sub[i] setImage:imageOriginal];
        NSLog(@"ImageButton: %@", boton);
}

Unfortunately some way to pass the image that I store in the UIImage to the UIIbutton

    
asked by JCTimmypage 19.05.2017 в 18:00
source

1 answer

0

Finally I found how to solve the problem.

In general, if a property is not specified for a state, the default value is to use the UIControlStateNorma l value. If the UIControlStateNormal value is not set, the default property is a system value . Therefore, at a minimum, you must set the value for the normal state, that is:

[sub[i] setImage:imageOriginal forState:UIControlStateNormal];
    
answered by 19.05.2017 / 18:16
source