¿Cómo podría dividir un UIImage en la mitad (en el centro) para que hiciera dos imágenes?Divida UIImage por la mitad?
Respuesta
Puede probar esto,
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
CGImageRef tmpImgRef = image.CGImage;
CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height/2.0));
UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
CGImageRelease(topImgRef);
CGImageRef bottomImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, image.size.height/2.0, image.size.width, image.size.height/2.0));
UIImage *bottomImage = [UIImage imageWithCGImage:bottomImgRef];
CGImageRelease(bottomImgRef);
esperanza de que esto le puede ayudar, :)
Respuesta de otra pregunta de Stackoverflow. Source
UIImage *splitMeInTwo = [UIImage imageNamed:@"Star.png"];
UIImageView *original = [[UIImageView alloc] initWithImage:splitMeInTwo];
[self.view addSubview:original];
[original release];
// The size of each part is half the height of the whole image
CGSize size = CGSizeMake([splitMeInTwo size].width, [splitMeInTwo size].height/2);
// Create image-based graphics context for top half
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
// Draw into context, bottom half is cropped off
//[image drawAtPoint:CGPointMake(0.0,0.0)];
[splitMeInTwo drawAtPoint:CGPointMake(0.0,0.0) blendMode:kCGBlendModeNormal alpha:1.0];
// Grab the current contents of the context as a UIImage
// and add it to our array
UIImage *top = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *topV = [[UIImageView alloc] initWithImage:top];
CGRect frameTop = topV.frame;
frameTop.origin.x += 360.0f;
topV.frame = frameTop;
[self.view addSubview:topV];
[topV release];
UIGraphicsEndImageContext();
- (void)splitImage:(UIImage *)image
{
CGFloat imgWidth = image.size.width/2;
CGFloat imgheight = image.size.height;
CGRect leftImgFrame = CGRectMake(0, 0, imgWidth, imgheight);
CGRect rightImgFrame = CGRectMake(imgWidth, 0, imgWidth, imgheight);
CGImageRef left = CGImageCreateWithImageInRect(image.CGImage, leftImgFrame);
CGImageRef right = CGImageCreateWithImageInRect(image.CGImage, rightImgFrame);
// These are the images we want!
UIImage *leftImage = [UIImage imageWithCGImage:left];
UIImage *rightImage = [UIImage imageWithCGImage:right];
// Don't forget to free the memory!
CGImageRelease(left);
CGImageRelease(right);
}
Esta es la única respuesta que no crea un montón de contextos. Quartz hace un gran trabajo (obteniendo los datos de píxeles de la imagen directamente, en lugar de crear un nuevo contexto, dibujar una imagen y luego recortar) para optimizar el proceso de recorte. ¡Esto es potencialmente muchas veces más rápido cuando estamos dividiendo imágenes en muchas partes pequeñas! –
IOS 11, SWIFT 4.0 Versión Actualizada https://stackoverflow.com/users/893872/durul-dalkanat que creo que para ser honesto es el mejor :)
Esto divide una imagen en 4 partes
func splitImage(image2D: UIImage) -> [UIImage] {
let imgWidth = image2D.size.width/2
let imgHeight = image2D.size.height/2
var imgImages:[UIImage] = []
let leftHigh = CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight)
let rightHigh = CGRect(x: imgWidth, y: 0, width: imgHeight, height: imgHeight)
let leftLow = CGRect(x: 0, y: imgHeight, width: imgWidth, height: imgHeight)
let rightLow = CGRect(x: imgWidth, y: imgHeight, width: imgWidth, height: imgHeight)
let leftQH = image2D.cgImage?.cropping(to:leftHigh)
let rightHQ = image2D.cgImage?.cropping(to:rightHigh)
let leftQL = image2D.cgImage?.cropping(to:leftLow)
let rightQL = image2D.cgImage?.cropping(to:rightLow)
imgImages.append(UIImage(cgImage: leftQH!))
imgImages.append(UIImage(cgImage: rightHQ!))
imgImages.append(UIImage(cgImage: leftQL!))
imgImages.append(UIImage(cgImage: rightQL!))
return imgImages
}
- 1. UIImage a CIImagen, el tamaño se reduce a la mitad?
- 2. PHP Incremento por la mitad
- 3. ¿Divida el tiempo por 2?
- 4. Dividir una tabla por la mitad
- 5. SSL es mitad simétrica y mitad asimétrica?
- 6. Separar cadenas por la mitad (Word-Aware) con PHP
- 7. Empalme una matriz por la mitad, sin importar el tamaño?
- 8. Divida el texto en páginas y presente por separado (HTML5)
- 9. Capture UIView como UIImage
- 10. Ordenar un array con la primera mitad y la segunda mitad ordenadas
- 11. Crear nueva UIImage añadiendo a la sombra UIImage existente
- 12. Divida el contenedor en trozos, C++
- 13. ¿Alrededor de la mitad peniques hacia arriba?
- 14. Seleccione solo la mitad de los registros
- 15. Dividir una lista en la mitad
- 16. Agregar UIImage encima de otro UIImage
- 17. Divida un diccionario en las variables
- 18. gmaps4rails muestra solo la mitad del mapa cuando la pestaña iside. ¿Alguna idea de por qué?
- 19. Encontrar la mitad de la lista de tamaños desconocidos
- 20. Comparando UIImage
- 21. ¿Rotar UIImage?
- 22. Creando un rectángulo dibujable en xml con un degradado en la mitad superior y otro en la mitad inferior
- 23. ajustar el ancho de la pantalla a la mitad
- 24. La mitad de la página + alineación de CSS
- 25. rotar CATransform3D hace que la mitad de la imagen desaparezca
- 26. iPhone SDK - ¿Cómo dibujar un UIImage en otro UIImage?
- 27. Cómo establecer la imagen en UIImage
- 28. UIImage + RoundedCorner con SDWebImage
- 29. extraño "a la mitad, incluso" redondeo en diferentes idiomas
- 30. Visualizar UIImage desde la cámara en UIWebView
Esto funcionó de maravilla. ¡Gracias! – Shredder2794