理想情况下,NSCoding兼容类将使用encodeWithCoder:和initWithCoder按预期工作:(至少我认为直到最近),开发人员不必担心例程内部的内容(除非我对NSCoding兼容类的想法完全搞砸了) !)
UIImageView类符合NSCoding.所以我不应该打扰如何使用NSKeyedArchiver和NSKeyedUnarchiver类对它进行序列化/反序列化.但每次我尝试编码UIImageView对象时,都会收到UIImage无法识别的错误:encodeWithCoder:method.
现在UIImageView内部使用UIImage对象.但编码是否应该自己处理?
或者文档中指定的NSCoding合规性是否让用户知道他们可以实现initWithCoder和encodeWithCoder方法?
有人可以帮我澄清一下!我很困惑!
我有一个小的iPhone应用程序正在处理我正在使用UIImageView显示图像.我正在使用Aspect Fit模式进行扩展.我想让图像按比例放大而不进行插值/平滑(我希望它看起来像素化).有什么方法可以改变这种行为吗?
一个更普遍的问题是我可以实现我自己的缩放算法,还是有其他内置的我可以选择?
我需要你的建议:我想知道如何在UIImageView中设置UIButton的backgroundImage图像.
即如果我触摸按钮,则应在UIImageView上设置按钮的背景图像.
可能吗?如果是,那怎么样?
如何通过在手指上滑动来擦除前面UIImage上的内容并显示UIView背面的UIImage.
我曾使用以下代码- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 擦除前图像:
-(void)eraseDrawingContents
{
UIGraphicsBeginImageContext(frontImage.frame.size);
[frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)];
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 0), 50, [[UIColor whiteColor]CGColor]);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, lastPoint.x, lastPoint.y);
CGPathAddLineToPoint(path, nil, currentPoint.x, currentPoint.y);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextAddPath(UIGraphicsGetCurrentContext(), path);
CGContextStrokePath(UIGraphicsGetCurrentContext());
frontImage.image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
Run Code Online (Sandbox Code Playgroud)
我实现了以下输出.
但我需要像这样的输出.

我们如何实现这一功能?
请帮我.
我有一个目标动作,当按下按钮时,我验证UITextField:
// Get the text from the UITextField
NSString *nameStr = _name.text;
_name.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
[self.view addSubview:_name];
if (nameStr.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Name"
message:@"You must enter a name."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
Run Code Online (Sandbox Code Playgroud)
我UITextField被添加到视图中,viewDidLoad如下所示:
[self.view addSubview:_name];
Run Code Online (Sandbox Code Playgroud)
如何UIImageView显示rightView ?
我正在尝试将用户上传的图像从一个UIImageView移动到另一个View Controller上的另一个UIImageView.我可以让用户上传图像并将其保存在第一个UIImageView中,但在按下"发布"按钮后,下一个视图控制器上的UIImageView仍为空白.
注意:browsingImage是第二个视图控制器上的UIImageView的名称(目标UIImageView)
任何帮助将不胜感激!
@IBAction func cameraButton(sender: AnyObject) {
addNewPicture()
}
func addNewPicture() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
postingImage.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func postButton(sender: AnyObject) {
performSegueWithIdentifier("toBrowsePage", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
var itemToAdd = segue.destinationViewController as! …Run Code Online (Sandbox Code Playgroud) 我的UIImageView加载了一个高分辨率的图片.当我将UIImageView添加到UIStackView时,堆栈视图将增长到1900x1200维度.UIImageView contentMode设置为Aspect Fill.
在将图像添加到堆栈视图后,我该怎么做才能使图像保持当前尺寸(130x130)?
我想UIImageView在里面添加一个动画,UICollectionViewCell所以我想出了这段代码:
import UIKit
class MainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var items:[String] = ["one", "two", "three", "four"]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0)
self.view.addSubview(self.collectionView)
}
lazy var collectionView:UICollectionView = {
var cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: self.flowLayout)
cv.delegate = self
cv.dataSource = self
cv.bounces = true
cv.alwaysBounceVertical = true
cv.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
cv.registerClass(CustomCell.self, forCellWithReuseIdentifier: "cell")
cv.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0)
return …Run Code Online (Sandbox Code Playgroud) 问题:
我有一个CollectionView,它将UIimage加载到每个单元格中.但是我的问题是,当我加载包含更多图像的其他单元格时,它们似乎会重复.我无法在我的代码中看到可能导致这种情况的原因.可能是因为可重复使用的细胞存在一些问题?
谁能明白为什么会这样?
注意:带有图像的阵列没有重复项
问题视频: https ://www.youtube.com/watch?v = vjRsFc8DDmI
问题形象:
这是我的collectionView函数:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
if self.movies == nil
{
return 0
}
return self.movies!.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell =
collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,
forIndexPath: indexPath) as! UpcomingCollectionViewCell
if self.movies != nil && self.movies!.count >= indexPath.row
{
// Calc size of cell
cell.frame.size.width = screenWidth …Run Code Online (Sandbox Code Playgroud) 我写了一个UICropperViewController,它在横向模式下完美地适用于图像.纵向模式下的图像存在很大问题.下图显示了带有黄色裁剪框的简单图片:
裁剪结果是:
现在谈到肖像图像我们得到了这种情况:
结果如下:
那么这里发生了什么?原始图像自动向左旋转.
我研究了很多,基本上找到了两个建议:
建议1
在裁剪之前保存图像方向并恢复它.
func didTapCropButton(sender: AnyObject) {
let originalOrientation = self.imageView.image?.imageOrientation;
// raw value of originalOrientation is `3` so its rotated to the right
let croppedCGImage = self.imageView.image?.cgImage?.cropping(to: self.cropArea);
// create a cropped cgImage
let croppedImage = UIImage(cgImage: croppedCGImage!, scale: (self.imageView.image?.scale)!, orientation: (originalOrientation)!);
// create the UIImage with the result from cgImage cropping and original orientation
if (self.callback != nil) {
self.callback.croppingDone(image: croppedImage);
}
self.dismiss(animated: true, completion: nil);
}
Run Code Online (Sandbox Code Playgroud)
但结果现在是:
显然这个建议不起作用,因为它只是简单地旋转已经裁剪的图像.
建议2
定位修复.我找到了以下代码,并承诺会修复错误: …
uiimageview ×10
ios ×7
iphone ×4
swift ×4
objective-c ×2
uibutton ×2
animation ×1
autolayout ×1
cgcontext ×1
cgimage ×1
ios7 ×1
nscoding ×1
scaling ×1
uiimage ×1
uikit ×1
uistackview ×1
uitextfield ×1
uiview ×1