如何使用objective-c从数组中选择不同的图像
Posted
技术标签:
【中文标题】如何使用objective-c从数组中选择不同的图像【英文标题】:how to select the images differently from array using objective-c 【发布时间】:2017-10-04 07:10:52 【问题描述】:我在 NSArray 中存储了 10 张图像。 在 UIViewController 我放置了 6 个 UIImageView,并在 .h 文件中给出了出口。
我需要从数组中选择任意6张图片怎么办? 并且需要在 UIImageView 中显示图片。
- (void)viewDidLoad
[super viewDidLoad];
images=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"], nil];
NSString *dd=[NSString stringWithFormat:@"%@", images];
NSLog(@"%@",dd);
- (void)setImagesForImageViews:(NSArray<UIImageView*>*)viewsArray fromArray:(NSArray<UIImage*>*)imageArray
if(images == nil || viewsArray == nil || viewsArray.count > images.count)
return; // early return
NSMutableArray* randomImageArray = [NSMutableArray arrayWithCapacity:viewsArray.count];
while (randomImageArray.count < viewsArray.count)
UIImage* image;
do
NSUInteger random = (NSUInteger)arc4random_uniform(images.count);
image = [imageArray objectAtIndex:random];
while ([randomImageArray containsObject:image]);
[randomImageArray addObject:image];
[viewsArray enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
obj.image = [randomImageArray objectAtIndex:idx];
];
在.h文件中
#导入
@interface memory_test : UIViewController
NSMutableArray *images;
@property(weak,nonatomic)IBOutlet UIImageView *b1;
@property(weak,nonatomic)IBOutlet UIImageView *b2;
@property(weak,nonatomic)IBOutlet UIImageView *b3;
@property(weak,nonatomic)IBOutlet UIImageView *b4;
@property(weak,nonatomic)IBOutlet UIImageView *b5;
@property(weak,nonatomic)IBOutlet UIImageView *b6;
【问题讨论】:
随机设置任意三张图片? 问题不清楚。要显示 3 张随机图片吗? 不清楚您的要求。请提及您做了什么以及您面临的问题。 SO 不是用来问关于howtos的问题 【参考方案1】:让我们假设(因为问题不清楚),您想将随机图像设置为图像数组中的图像视图。这里的挑战是从数组中找到随机图像,不重复。
我还没有测试过代码,但是这样的东西应该可以工作:
- (void)viewDidLoad
[super viewDidLoad];
NSArray* images = [self imagesArray];
NSArray* views = [self imageViewsArray];
[self setRandomImagesForImageViews:views fromArray:images];
- (NSArray<UIImage*>*)imagesArray
return @[[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"]];
- (NSArray<UIImageView*>*)imageViewsArray
return @[_b1, _b2, _b3, _b4, _b5, _b6];
- (void)setRandomImagesForImageViews:(NSArray<UIImageView*>*)viewsArray fromArray:(NSArray<UIImage*>*)imageArray
if(imageArray == nil || viewsArray == nil || viewsArray.count > imageArray.count)
return; // early return
NSMutableArray* randomImageArray = [NSMutableArray arrayWithCapacity:viewsArray.count];
while (randomImageArray.count < viewsArray.count)
UIImage* image;
do
NSUInteger random = (NSUInteger)arc4random_uniform(imageArray.count);
image = [imageArray objectAtIndex:random];
while ([randomImageArray containsObject:image]);
[randomImageArray addObject:image];
[viewsArray enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
obj.image = [randomImageArray objectAtIndex:idx];
];
如果您不再在课堂上使用它,您可以从头文件中删除您的图像数组声明。
【讨论】:
我有 6 个 UIImageview 那么如何设置 您可以通过将 imageview 出口保存在数组中来使其更通用。 @teena:改了代码,你只需要把images数组和imageview数组传给这个函数。 视图中如何调用这个函数 @teena:我猜你已经有了一个图像视图数组。您需要创建一个图像视图数组,然后使用图像视图数组和图像数组调用此函数。【参考方案2】:适用于 SWIFT
class ViewController: UIViewController
//array with ImageView
@IBOutlet var imageViews: [UIImageView]!
//array with Image
lazy var imagesArray = [UIImage]()
override func viewDidLoad()
super.viewDidLoad()
//initiate image
let imageOne :UIImage = #imageLiteral(resourceName: "exchange")
let imageTwo :UIImage = #imageLiteral(resourceName: "indexPhone")
let imageThree :UIImage = #imageLiteral(resourceName: "indexCheer")
let imageFour :UIImage = #imageLiteral(resourceName: "indexTrade")
let imageFive :UIImage = #imageLiteral(resourceName: "indexWashMachine")
//add image to array
imagesArray = [imageOne,imageTwo,imageThree,imageFour,imageFive]
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
//Action for Button
@IBAction func randomSelect(_ sender: Any)
//temporary array for images
var tempImageArray = imagesArray
for imageView in imageViews
// get random index for array images
let index = Int(arc4random_uniform(UInt32(tempImageArray.count)))
// set random image for ImageView
imageView.image = tempImageArray[index]
// remove arleady added Image from temporary array
tempImageArray.remove(at: index)
针对目标-C
@interface ViewController ()
//array with ImageView
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageViews;
//array with Image
@property (strong, nonatomic) NSArray* imagesArray;
- (IBAction)randomSelect:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
//initiate image
UIImage *imageOne = [UIImage imageNamed:@"exchange"];
UIImage *imageTwo = [UIImage imageNamed:@"indexCheer"];
UIImage *imageThree = [UIImage imageNamed:@"indexPhone"];
UIImage *imageFour = [UIImage imageNamed:@"indexTrade"];
UIImage *imageFive = [UIImage imageNamed:@"indexWashMachine"];
//Initiate array with image
self.imagesArray = [NSArray arrayWithObjects:imageOne,imageTwo,imageThree,imageFour,imageFive, nil];
// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)randomSelect:(id)sender
//temporary array for images
NSMutableArray* tempImageArray = [[NSMutableArray alloc]initWithArray:self.imagesArray];
for (UIImageView* imageView in self.imageViews)
// get random index for array images
int index = arc4random()%(tempImageArray.count-1);
// set random image for ImageView
imageView.image = [tempImageArray objectAtIndex:index];
// remove arleady added Image from temporary array
[tempImageArray removeObjectAtIndex:index];
【讨论】:
我需要objective-c 5分钟,我正在为obj-c重新制作 我有 6 个 UIImageview 那么如何设置 您的 Image 数量大于 ImageView 的数量? 如果计算 imageView 是否超过 Image ,请评论此行“[tempImageArray removeObjectAtIndex:index]”以上是关于如何使用objective-c从数组中选择不同的图像的主要内容,如果未能解决你的问题,请参考以下文章
Objective-C中NSArray和NSMutableArray是如何使用的?
如何从 SQLite 中的表中选择最新的 100 个不同条目?