puso pantalla negativo invertir hora como colores color cambio cambiar iphone colors crash uiimage

iphone - pantalla - Convirtiendo uiimage a colores negativos 2da vez se cuelga la aplicación



mi iphone cambio de color la pantalla (3)

Lanzas la imagen dos veces. UIGraphicsGetImageFromCurrentImageContext () devuelve un objeto liberado automáticamente, por lo que no lo suelte antes de abandonar la función. El grupo de autorrelease lo hará por usted cuando la ejecución regrese al ciclo de ejecución.

Quiero mostrar una animación con una imagen negativa y una imagen normal. Para comenzar, se llama al método getPhoto y el usuario puede obtener una imagen de la biblioteca o tomar una con la cámara.

Cuando la animación se está ejecutando, quiero poder establecer la imagen en una nueva, pero luego la aplicación falla.

-(IBAction) getPhoto:(id) sender { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; if((UIButton *) sender == choosePhotoBtn) { picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } else { picker.sourceType = UIImagePickerControllerSourceTypeCamera; } [self presentModalViewController:picker animated:YES]; [picker release]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; UIImage *negative = [self convertImageToNegative:[info objectForKey:@"UIImagePickerControllerOriginalImage"]]; imageView.animationImages = [NSArray arrayWithObjects: negative,[info objectForKey:@"UIImagePickerControllerOriginalImage"],nil]; imageView.animationDuration = 60.0; // seconds imageView.animationRepeatCount = 1; // 0 = loops forever [imageView startAnimating]; [negative release]; } - (UIImage *)convertImageToNegative:(UIImage *)image{ UIGraphicsBeginImageContext(image.size); CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height)); UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return returnImage; }

Instrumentos: la muestra de CPU muestra que el uso es 100%. ¿Cómo puedo prevenir esto?


Este método le dará la imagen invertida de la imagen que especifique en el parámetro

-(UIImage *) getImageWithInvertedPixelsOfImage:(UIImage *)image { UIGraphicsBeginImageContextWithOptions(image.size, NO, 2); CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height)); UIImage * result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return result; }

Úselo así:

yourImage.image = [self getImageWithInvertedPixelsOfImage:yourImage.image];


La respuesta de Fernando Cervantes en Swift 2:

func getImageWithInvertedPixelsOfImage(image: UIImage) -> UIImage { let rect = CGRect(origin: CGPoint(), size: image.size) UIGraphicsBeginImageContextWithOptions(image.size, false, 2.0) CGContextSetBlendMode(UIGraphicsGetCurrentContext(), .Copy) image.drawInRect(rect) CGContextSetBlendMode(UIGraphicsGetCurrentContext(), .Difference) CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), UIColor.whiteColor().CGColor) CGContextFillRect(UIGraphicsGetCurrentContext(), rect) let result = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return result }