您的位置:首页 > 产品设计 > UI/UE

解决 UIView 设置背景为UIImage图片变型问题[XXX setBackgroundColor:

2014-03-25 11:47 501 查看
[self.drawingView setBackgroundColor:[UIColor colorWithPatternImage:[self thumbnailWithImageWithoutScale:imagesize:self.drawingView.frame.size]]];

- (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize

{

UIImage *newimage;

if (nil == image)

{

newimage = nil;

}

else

{

CGSize oldsize = image.size;

CGRect rect;

if (asize.width/asize.height > oldsize.width/oldsize.height)

{

rect.size.width = asize.height*oldsize.width/oldsize.height;

rect.size.height = asize.height;

rect.origin.x = (asize.width - rect.size.width)/2;

rect.origin.y = 0;

}

else

{

rect.size.width = asize.width;

rect.size.height = asize.width*oldsize.height/oldsize.width;

rect.origin.x = 0;

rect.origin.y = (asize.height - rect.size.height)/2;

}

UIGraphicsBeginImageContext(asize);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[UIColorclearColor] CGColor]);

UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background

[image drawInRect:rect];

newimage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

return newimage;

}
转载:http://www.cnblogs.com/Camier-myNiuer/archive/2013/12/03.html

Animating UIImageView with colorWithPatternImage

Leave your code as it is, but instead using
UIImageView
use
my subclass:
//
//  AnimatedPatternView.h
//

#import <UIKit/UIKit.h>

@interface AnimatedPatternView : UIImageView;
@end


//
//  AnimatedPatternView.m
//

#import "AnimatedPatternView.h"

@implementation AnimatedPatternView

-(void)setAnimationImages:(NSArray *)imageArray
{
NSMutableArray* array = [NSMutableArray arrayWithCapacity:imageArray.count];

for (UIImage* image in imageArray) {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0);
UIColor* patternColor = [UIColor colorWithPatternImage:image];
[patternColor setFill];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextFillRect(ctx, self.bounds);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[array addObject:img];

}
[super setAnimationImages:array];
}

@end


If you create the view using interface builder you only need to set the class of the image view in the identity inspector.

转载:http://stackoverflow.com/questions/16461469/animating-uiimageview-with-colorwithpatternimage
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: