Nicolas Charvoz
Nicolas Charvoz

Reputation: 1509

Avoid white background in UIImageView

I have .png images, and my backgrounds are white. I wanted a quick solution to transform the white background in a transparent background (code solution) as the app bg is grey and not white. I already tried CSS (pixate) but it doesn't work.

-(void)getImageFromeRequestedUrl:(NSString*)urlRequested {
NSString *userName = @"userName";
NSString *password = @"passWord";
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlRequested]];
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", userName, password];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)];
[urlRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Response: %@", responseObject);
    self.image.image = responseObject;
    self.image.frame = CGRectMake(0, 0, 300, 300);
    self.image.center = self.image.superview.center;
    self.image.backgroundColor = [UIColor clearColor];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Image error: %@", error);
}];
[requestOperation start];
}

Upvotes: 0

Views: 3202

Answers (3)

DeFrenZ
DeFrenZ

Reputation: 2262

You may need to also set the opaque property to NO

Upvotes: 0

Kegluneq
Kegluneq

Reputation: 796

If what you mean is that actual physical image background is white then here is possible solution using UIImageView. But bear in mind that this will work only if background is really white (as in rgb #ffffff). Also, if some other element in the image is white, it will also be colored (or in your case, disappear). Oh, and you need at least iOS7.

UIImage *yourImage = ...
UIImageView *yourImageView = ...
...
yourImageView.image = [yourImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
yourImageView.tintColor = [UIColor clearColor];

But it would be much better if you just modified actual image in some graphics program yourself.

Upvotes: 1

E-Riddie
E-Riddie

Reputation: 14780

Issue

Your imageView backgroundColor I think is white, because is white by default. Also when you open your transparent .png images in a photoViewer their background looks white, but it's not like that. If you have a tool like PhotoShop, you can open it and see the difference.

Solution

To deal with transparent png images in UIImageView you need to set the imageView background color to transparent.

The clear color makes the imageView background transparent:

UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor clearColor];

Also try to change your view the background color to another color than white, if your white part changes color, then the image is transparent, and the configurations are made ok.

Upvotes: 1

Related Questions