2013-01-20 5 views
1

내가 뭘하려고했는지 UIImageView 어둡게 이미지 및 결과의 상단에있는보기가 있도록했다. 나는 ios6에서 사용자가 Facebook을 사용하여 공유 할 때 부여되는 효과를주고 싶었습니다. 그 뒤에있는 모든 콘텐츠는 더 어두워 지지만 표시됩니다. 사용해 보았습니다 :어둡게 UIImageView IOS

UIImage *maskedImage = [self darkenImage:image toLevel:0.5]; 

- (UIImage *)darkenImage:(UIImage *)image toLevel:(CGFloat)level 
{ 
// Create a temporary view to act as a darkening layer 
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
UIView *tempView = [[UIView alloc] initWithFrame:frame]; 
tempView.backgroundColor = [UIColor blackColor]; 
tempView.alpha = level; 

// Draw the image into a new graphics context 
UIGraphicsBeginImageContext(frame.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
[image drawInRect:frame]; 

// Flip the context vertically so we can draw the dark layer via a mask that 
// aligns with the image's alpha pixels (Quartz uses flipped coordinates) 
CGContextTranslateCTM(context, 0, frame.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextClipToMask(context, frame, image.CGImage); 
[tempView.layer renderInContext:context]; 

// Produce a new image from this context 
CGImageRef imageRef = CGBitmapContextCreateImage(context); 
UIImage *toReturn = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 
UIGraphicsEndImageContext(); 
return toReturn; 
} 

그러나 원하는 효과를 내지 못합니다. 그것은 ios facebook 공유를 사용할 때처럼 어두워지기를 원하는 반면, 색상은 바뀌고 배경은 어두워집니다. 이것이 가능한가?

+4

알파를 적절한 수준으로 설정하여 맨 위에 새 (검은 색 채우기)보기를 배치하지 않는 이유는 무엇입니까? (추측 하건데, 이것은 UIImage를 조작하는 것보다 자원 집약적 인 것 같아요. 예, 이것은 필요한 테스트 없이는 의미가 없지만 거기에 있습니다.) –

+0

@middaparka가 제시 한 제안을 사용했습니다. 그리고 그것은 괜찮습니다 (당신이 페이스 북 공유를 통해 얻을 수있는 멋진 방사형 그래디언트를 원하지 않는 한). 원하는 경우 맞춤보기를 만들어 그려야합니다. – petemorris

+0

@middaparka 질문에 답을하고 코멘트에 답을 쓰는 것보다는 간단히 대답하는 것이 어떻겠습니까? – Mecki

답변

8

모든 내용과 하위보기가 더 어둡게 만들려면 UIView를 모두 검정색으로 칠하고 알파 투명도를 지정하여이 "차폐보기"의 검정색이 아래보기. 알파 투명도가 0.5이면 아래의 모든 항목이 50 % 어두워 보입니다.

그냥 "차폐보기"의 전환을 사용, 페이딩 효과를 얻으려면 :

// Make the view 100% transparent before you put it into your view hierarchy. 
[myView setAlpha:0]; 

// Layout the view to shield all the content you want to darken. 
// Give it the correct size & position and make sure is "higher" in the view 
// hierarchy than your other content. 
...code goes here... 

// Now fade the view from 100% alpha to 50% alpha: 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[myView setAlpha:0.5]; 
[UIView commitAnimations]; 

당신은 페이드 느리게 또는 빠르게 필요로 할 애니메이션 기간을 변경할 수 있습니다. 애니메이션 지속 시간을 설정하지 않으면 기본 지속 시간이 사용됩니다 (내가 추측하는 Facebook 공유와 같은 경우).

+0

Geat 아이디어 덕분에 좋은 페이 스북 효과를주지 못하더라도 – Alessandro

+1

@Alessandroz : 왜 그런 페이스 북 효과를주지 않습니까? 전환 (페이딩 효과) 때문에? 이 효과는 쉽게 만들 수 있습니다. 차폐 뷰의 알파를 0에서 0.5로 희미하게 만드십시오. 질문을 업데이트하고 코드를 게시하겠습니다. – Mecki

관련 문제