解決 UIView 設定背景為UIImage圖片變型問題[XXX setBackgroundColor:
[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