GVKun编程网logo

如何在Swift中将gif显示到UIImageView中?(swiftui gif)

11

本文将介绍如何在Swift中将gif显示到UIImageView中?的详细情况,特别是关于swiftuigif的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉

本文将介绍如何在Swift中将gif显示到UIImageView中?的详细情况,特别是关于swiftui gif的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于ios – Swift UIImageView拉伸方面、ios – swift,如何从UIImageView获取当前显示的图像文件名、iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误)、ios – 在Swift中,如何在TableView中的TableViewCell中添加UIImageView上的徽章?的知识。

本文目录一览:

如何在Swift中将gif显示到UIImageView中?(swiftui gif)

如何在Swift中将gif显示到UIImageView中?(swiftui gif)

我想在UIImageView中使用gif,但不能。我的代码是:

- (void)viewDidLoad {    [super viewDidLoad];    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"];    UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];    self.dataImageView.animationImages = testImage.images;    self.dataImageView.animationDuration = testImage.duration;    self.dataImageView.animationRepeatCount = 1;    self.dataImageView.image = testImage.images.lastObject;    [self.dataImageView startAnimating];}

迅速:

var url : NSURL = NSBundle.mainBundle().URLForResource("MAES", withExtension: "gif")

在客观CI中有animationImageWithAnimatedGIFData,但是很快我不知道怎么称呼它,或者不存在。

谢谢!

答案1

小编典典

我假设您使用的是https://github.com/mayoff/uiimage-from-animated-
gif,+UIImageanimatedImageWithAnimatedGIFData:源代码在Objective-C中。

您首先需要将Objective-C标头导入Swift。 将Swift与Cocoa和Objective-
C结合使用

说明了如何做到这一点:

要在与Swift代码相同的框架目标中导入一组Objective-C文件,您需要将这些文件导入框架的Objective-C伞形标头中。

从同一框架将Objective-C代码导入Swift

  1. 在“打包设置”下的“构建设置”下,确保该框架目标的“定义模块”设置被设置为“是”。
  2. 在您的伞形头文件中,导入要公开给Swift的每个Objective-C头。例如:

#import "UIImage+animatedGIF.h"

然后,您可以testImage按照以下步骤进行设置:

var testImage = UIImage.animatedImageWithAnimatedGIFData(    NSData.dataWithContentsOfURL(url))self.dataImageView.animationImages = testImage.imagesself.dataImageView.animationDuration = testImage.durationself.dataImageView.animationRepeatCount = 1self.dataImageView.image = testImage.images.lastObjectself.dataImageView.startAnimating()

ios – Swift UIImageView拉伸方面

ios – Swift UIImageView拉伸方面

UI ImageView不正确地渲染图像的大小.使用Scale Aspect Fit,如果UIImageView是一个正方形,图像是正确的宽高比,透明度在图像不填充的区域.
//Image is Square & Correct Size
var imageView = UIImageView(frame: CGRectMake(0,50,320,320))
imageView.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFit

//Image is Rectangle & Incorrect Size
var imageView = UIImageView(frame: CGRectMake(0,450))
imageView.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFit

UIImageView需要触摸边缘,并且在屏幕的顶部和底部具有透明空间,并且内部的图像需要保持其原始比例而不是拉伸更高.我附上了UIImageView中图像呈现的两个图像.

解决方法

我向UIImageView添加了一个自动调整蒙版,现在显示正确的比例.
imageView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibletopMargin | UIViewAutoresizing.FlexibleWidth
imageView.contentMode = UIViewContentMode.ScaleAspectFit

这个答案帮助我:Captured photo is stretched with AVCaptureSession sessionPreset = AVCaptureSessionPresetPhoto,因为我正在使用通过手机相机拍摄的图像.

ios – swift,如何从UIImageView获取当前显示的图像文件名

ios – swift,如何从UIImageView获取当前显示的图像文件名

我想获取当前在UI ImageView上显示的图像文件名.我试着得到如下:
let currentimage = alien.image // !alien is my image view 
println(currentimage?.description)

但它打印:

Optional("<UIImage: 0x7fa61944c3d0>")

解决方法

你不能这样做.既不快速也不客观 – c.

要做的是存储要检索的数据.那就是……将名称存储在某处并使用它来加载图像.不是相反.

因此,创建一个类似于imageName的属性,然后使用它来加载图像.

iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误)

iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误)

我以前一直在从Parse后端检索图像,使用以下代码行在UI ImageView中的应用程序中显示:
let userPicture = PFUser.currentUser()["picture"] as PFFile

userPicture.getDataInBackgroundWithBlock { (imageData:NSData,error:NSError) -> Void in
    if (error == nil) {

            self.dpImage.image = UIImage(data:imageData)

    }
}

但我得到错误:

‘AnyObject?’ is not convertible to ‘PFFile’; did you mean to use ‘as!’
to force downcast?

“有用的”Apple修复技巧提示“as!”改变所以我添加!,但后来我得到错误:

‘AnyObject?’ is not convertible to ‘PFFile’

使用’getDataInBackgroundWithBlock’部分,我也得到错误:

Cannot invoke ‘getDataInBackgroundWithBlock’ with an argument list of type ‘((NSData,NSError) -> Void)’

有人可以解释如何从Parse正确检索照片并使用Swift 1.2在UIImageView中显示它吗?

解决方法

PFUser.currentUser()返回可选类型(Self?).因此,您应该将返回值解包为按下标访问元素.
PFUser.currentUser()?["picture"]

下标得到的值也是可选类型.因此,您应该使用可选绑定来转换值,因为类型转换可能会失败.

if let userPicture = PFUser.currentUser()?["picture"] as? PFFile {

getDataInBackgroundWithBlock()方法的结果块的参数都是可选类型(NSData?和NSError?).所以你应该为参数指定可选类型,而不是NSData和NSError.

userPicture.getDataInBackgroundWithBlock { (imageData: NSData?,error: NSError?) -> Void in

修改上述所有问题的代码如下:

if let userPicture = PFUser.currentUser()?["picture"] as? PFFile {
    userPicture.getDataInBackgroundWithBlock { (imageData: NSData?,error: NSError?) -> Void in
        if (error == nil) {
            self.dpImage.image = UIImage(data:imageData)
        }
    }
}

ios – 在Swift中,如何在TableView中的TableViewCell中添加UIImageView上的徽章?

ios – 在Swift中,如何在TableView中的TableViewCell中添加UIImageView上的徽章?

我正在开发一个社交网站,我有一个带有多个单元格(行)的TableView,在每个单元格中我有一个“注释”和“喜欢” Imageview,所以每当更新来自服务器时我都要增加“注释”的数量“和”喜欢“通过徽章.那么如何在 Swift中的TableViewCell中添加UIImageView上的徽章

解决方法

您可以通过这种方式在UIImageView上添加徽章

func drawImageView(mainImage: UIImage,withBadge badge: UIImage) -> UIImage
{
    UIGraphicsBeginImageContextWithOptions(mainImage.size,false,0.0)
    mainImage.drawInRect(CGRectMake(0,mainImage.size.width,mainImage.size.height))
    badge.drawInRect(CGRectMake(mainImage.size.width - badge.size.width,badge.size.width,badge.size.height))

    let resultimage: UIImage = UIGraphicsGetimageFromCurrentimageContext()
    UIGraphicsEndImageContext()
    return resultimage
}

然后使用此功能

let mainImage = UIImage(named: "main_image_name_here")
    let badgeImage = UIImage(named: "badge_icon_name_here")
    let myBadgedImage: UIImage = drawImageView(mainImage!,withBadge: badgeImage!)
    myImageView.image = myBadgedImage//set Image on ImageView

今天关于如何在Swift中将gif显示到UIImageView中?swiftui gif的介绍到此结束,谢谢您的阅读,有关ios – Swift UIImageView拉伸方面、ios – swift,如何从UIImageView获取当前显示的图像文件名、iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误)、ios – 在Swift中,如何在TableView中的TableViewCell中添加UIImageView上的徽章?等更多相关知识的信息可以在本站进行查询。

本文标签: